What Is TypeScript and Why Use It
TypeScript is JavaScript with a type system that catches bugs before your code runs. Learn what it is, how it works, and why developers choose it.
TypeScript is JavaScript with a type system. You write JavaScript as usual, but you can also describe what shape your data should have. The TypeScript compiler checks your descriptions against your code and tells you when something does not match, all before your code ever runs.
The key word is before. In plain JavaScript, you find out about a typo in a property name or a missing function argument when the code crashes at runtime. TypeScript catches those mistakes while you are still writing.
How TypeScript Works
It adds a compile step before your code reaches the browser or Node.js:
The compiler reads your .ts files, checks all the types, and produces plain .js files. If there is a type error, the compiler tells you before producing JavaScript.
The types disappear completely during compilation, and the browser never sees them. This is the most important thing to understand: types are compile-time only. When your code runs, it is just JavaScript.
A First Look at TypeScript
Here is the smallest example that shows what it adds:
function greet(name: string) {
return `Hello, ${name}!`;
}
console.log(greet("World"));
console.log(greet(42));The : string after name tells the compiler that name must be a string. The first call works fine. The second call produces this compiler error:
Argument of type 'number' is not assignable to parameter of type 'string'.This error appears in your editor and during compilation. Your code never runs with the wrong type. In plain JavaScript, greet(42) would produce "Hello, 42!" and you might never notice the mistake.
Here is what the compiled JavaScript looks like:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("World"));
console.log(greet(42));The string annotation is gone. The JavaScript is identical to what you would write by hand. TypeScript added a safety check without changing the runtime behavior at all.
Why Developers Choose TypeScript
TypeScript solves a real problem: JavaScript gives you very little feedback while you write code. You can misspell a property, pass the wrong number of arguments, or forget that a value might be undefined, and JavaScript will not tell you until that line executes. It surfaces those problems immediately instead.
| Problem | JavaScript behavior | TypeScript behavior |
|---|---|---|
| Misspelled property | Returns undefined, may crash later | Compiler error at the misspelling |
| Wrong argument type | Runs with unexpected behavior | Compiler error at the call site |
| Possibly-null value | Runtime TypeError | Compiler requires a null check |
| Missing function argument | Undefined passed silently | Compiler error if parameter is required |
This feedback loop is faster and safer. You fix bugs as you type instead of discovering them in production.
It also powers editor features you may already use:
- Autocomplete that knows exactly which properties exist on an object.
- Go-to-definition that jumps straight to where a function or type is declared.
- Rename refactoring that safely renames a variable across an entire project.
These features work because the type system gives the editor a complete picture of your code.
The Gradual Adoption Story
You do not need to convert an entire project at once. Because every JavaScript file is already valid TypeScript, you can rename one file from .js to .ts and start adding types to just that file. This means teams can adopt TypeScript incrementally:
- Rename app.js to app.ts. Nothing breaks.
- Add a
: stringannotation to one function parameter. The compiler starts helping. - Enable stricter compiler options one at a time as the team gets comfortable.
You control the pace. TypeScript has compiler options like strict, noImplicitAny, and strictNullChecks that let you decide how much type safety you want. Most projects eventually use full strict mode, but you do not start there.
For more on compiler configuration, see TypeScript compiler explained.
TypeScript vs JavaScript in Practice
TypeScript is not a different language. It is the same JavaScript you already know, plus a type system that helps you write safer code. The difference shows up in everyday coding tasks:
- Calling a function: JavaScript lets you pass anything. TypeScript checks that arguments match the expected types.
- Accessing an object property: JavaScript returns undefined for a typo. TypeScript tells you the property does not exist.
- Handling a value that might be missing: JavaScript crashes with a runtime error. TypeScript forces you to handle the missing case.
For a detailed side-by-side comparison, see TypeScript vs JavaScript explained.
Is TypeScript Right for You
It is useful if you want:
- Fewer runtime bugs caused by type mismatches.
- Better editor support with autocomplete and inline errors.
- Code that is easier for teammates to read and refactor.
- The ability to adopt it gradually without rewriting everything.
It may not be the right fit if you are writing a tiny script that will run once and be thrown away, or if your team is not ready for the compile step in the workflow. For nearly everything else, it is worth the small upfront investment.
The next step is to see it catch bugs in action. Read how TypeScript helps catch bugs early to see real examples of errors it prevents before they reach your users.
Rune AI
Key Insights
- TypeScript is JavaScript with optional static types.
- The type system only exists at compile time, never at runtime.
- TypeScript catches bugs before your code runs, saving hours of debugging.
- All valid JavaScript is valid TypeScript, so you can adopt it gradually.
- It compiles to plain JavaScript that runs in any browser or Node.js.
Frequently Asked Questions
Is TypeScript a separate language from JavaScript?
Do browsers run TypeScript directly?
Do I need to learn JavaScript before TypeScript?
Conclusion
TypeScript takes JavaScript and adds a compile-time type system that catches mistakes before they reach production. You write types to describe what your code should do, and the compiler tells you when something is wrong. It does not change how JavaScript runs. It just makes you more confident that your code works the way you think it does.
More in this topic
TypeScript Compiler Explained
The TypeScript compiler (tsc) turns .ts files into .js files while checking types. Learn what it does, how to use it, and how it fits your workflow.
Set Up ESLint for TypeScript
Install and configure ESLint with typescript-eslint to catch bugs and enforce consistent code style in your TypeScript project.
Pick Utility Type in TypeScript
Pick creates a new type by selecting only the properties you name from an existing type. Use it to build focused types for specific views, API responses, or function parameters.