Lesson 99 of 296
What TypeScript is and why it exists
What you will learn
- What TypeScript is
- Static vs dynamic typing
- How TypeScript code becomes JavaScript
- Why types disappear when the code runs
- The real benefits, and the limits
- How to run TypeScript
On this page
What is TypeScript?
TypeScript (TS) is JavaScript plus a type system. It was created by Microsoft and is open source. Every valid JavaScript program is almost always valid TypeScript; TypeScript adds type annotations that describe what kind of values your variables, parameters and return values hold.
function add(a: number, b: number): number {
return a + b;
}
add(2, 3); fine
add("2", 3); Error: Argument of type 'string' is not assignable to parameter of type 'number'.The error appears in your editor while you type, before the code ever runs.
Static vs dynamic typing
| Dynamic typing (JavaScript) | Static typing (TypeScript) | |
|---|---|---|
| When type mistakes are found | When the code runs, often by a user | When you write or build the code |
| Variables have a fixed type | No | Yes |
| Example of a bug | user.nmae gives undefined at runtime | Error in the editor: property 'nmae' does not exist |
How TypeScript becomes JavaScript
Browsers and Node.js run JavaScript. So TypeScript is compiled (more precisely transpiled) into JavaScript:
- The TypeScript compiler (
tsc) checks the types and reports errors. - It removes the type annotations and outputs plain JavaScript.
TypeScript: function add(a: number, b: number): number { return a + b; }
JavaScript: function add(a, b) { return a + b; }This removal is called type erasure.
The most important fact: types do not exist at runtime
Because types are erased, TypeScript cannot check data that arrives while the program runs: API responses, form input, files, JSON.parse results. If an API says it returns a User but sends something else, TypeScript cannot know. You need runtime validation for that (Lesson 114, Zod).
This is a very common interview question: "Does TypeScript make your app type-safe at runtime?" The answer is no: it checks your code, not the data.
Why teams use TypeScript
- Fewer bugs: typos, wrong arguments, missing null checks are caught early.
- Better editor help: auto-complete, jump to definition, inline documentation.
- Safe refactoring: rename a property and every place that uses it is updated or flagged.
- Types are documentation that cannot go out of date.
- Easier teamwork on large codebases.
The costs
- A build or check step.
- A learning curve, especially for advanced types.
- It is possible to "lie" to the compiler with
anyandas(Lessons 102 and 113).
Running TypeScript
| Way | Use |
|---|---|
tsc | The official compiler. npx tsc --noEmit only checks types. |
tsx | Runs .ts files directly in Node.js during development: npx tsx src/index.ts |
| Bundlers and frameworks (Vite, Next.js, NestJS) | Handle TypeScript for you |
| Node.js type stripping | Recent Node.js versions can run many .ts files directly by removing the types. It does not check types, and some features (such as enums) are not supported in this mode. |
Starting a project:
npm install -D typescript
npx tsc --init creates tsconfig.json (Lesson 114)In most real projects, a tool such as esbuild, SWC or Vite removes the types quickly, and tsc --noEmit runs separately (for example in CI) to check them.
File extensions
| Extension | Meaning |
|---|---|
.ts | TypeScript |
.tsx | TypeScript with JSX (React) |
.d.ts | Declaration files: types only, no code. Describe the types of JavaScript libraries. |
Many libraries include their own types. For others, the community publishes them as @types/ packages: npm install -D @types/node.
Common mistakes
- Thinking TypeScript validates API data at runtime.
- Using
anywhenever an error appears. - Fighting the compiler instead of reading the error message, which usually explains the problem exactly.
How to say it in an interview
"TypeScript is a statically typed superset of JavaScript. The compiler checks types while I develop and then erases them, outputting plain JavaScript, so types do not exist at runtime. It catches mistakes early, powers editor tooling and makes refactoring safe, but it cannot validate external data like API responses, so I add runtime validation, for example with Zod, at those boundaries."
Key words
| Word | Meaning |
|---|---|
| TypeScript | JavaScript with a static type system |
| Type annotation | Writing a type, such as : number |
| Static typing | Types checked before the code runs |
| Compile / transpile | Turn TypeScript into JavaScript |
| Type erasure | Types being removed from the output |
| tsc | The TypeScript compiler |
| Declaration file | A .d.ts file containing only types |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.