What TypeScript is and how it works
TypeScript is a programming language that adds static typing to JavaScript: the types of values are checked before the program runs. The tsc compiler finds errors and emits plain JavaScript with the types removed.
TypeScript pays off where code grows and several people edit it. The check catches misspelt property names, wrong arguments and a forgotten null, and the editor uses the same types to suggest methods and find every usage. Any working JavaScript is also valid TypeScript syntax, so a project can be migrated one file at a time. TypeScript does not check data at run time and does not change how the code behaves: an API response or user input still has to be validated in code.
The compiler does two independent jobs. It checks types and prints errors, then erases the annotations (type erasure) and emits .js. By default, type errors do not stop the files from being emitted, so code being migrated to TypeScript keeps working. Node.js and bundlers can only erase types, so in projects where they run the code, run the check separately: npx tsc --noEmit.
app.js from being emitted, enable the noEmitOnError option.What remains after compilation
Annotations are written after the name, following a colon. They are absent from tsc output: the interface disappears entirely, while the function code stays the same.
interface User { name: string; id: number;} export function greet(user: User): string { return `Salom, ${user.name}!`;} console.log(greet({ name: "Aziza", id: 1 }));export function greet(user) { return `Salom, ${user.name}!`;}console.log(greet({ name: "Aziza", id: 1 }));Structural typing
Type compatibility is determined by the shape of a value (structural typing), not by the name of its type. An object fits wherever a Point is expected if it has x and y fields of the right types; extra fields do no harm. The exception is an object literal written directly in place of the value: an extra field there is treated as a typo.
interface Point { x: number; y: number;} function show(p: Point): string { return `${p.x}, ${p.y}`;} const marker = { x: 12, y: 26, label: "A" };console.log(show(marker)); // error TS2353: property label does not exist in type Point// const corner: Point = { x: 0, y: 0, label: "B" };12, 26How tsc differs from Node.js and transpilers
| Feature | tsc | Node.js | Babel, swc, Sucrase |
|---|---|---|---|
| Checks types | yes | no | no |
What it does with .ts | emits .js | replaces types with spaces and runs the file immediately | emits .js |
| Limitations | none | erasable syntax only: no enum or namespace with code | files are processed one at a time: enable the isolatedModules option in tsc |
Which tool to use: tsc is always the one that checks types, while Node.js and transpilers can run or build the code. They are faster because they do not check. If something other than tsc emits the .js in your project, add npx tsc --noEmit to CI, otherwise nobody will see type errors.
What you need to know first
- The JavaScript language: functions, objects, classes, modules and promises — see the JavaScript reference. TypeScript does not change how they behave; it only describes their types.
- Node.js and npm: installation,
package.json, running scripts — see the Node.js reference. - The command line: directories, running commands, environment variables — see the Bash reference.
Section sources
the section is written from these pages- TypeScript for JavaScript Programmerstypescriptlang.org/docs/handbook/typescript-in-5-minutes.html(opens in a new tab)
- TypeScript for the New Programmertypescriptlang.org/docs/handbook/typescript-from-scratch.html(opens in a new tab)
- The Basicstypescriptlang.org/docs/handbook/2/basic-types.html(opens in a new tab)
- Modules: TypeScriptnodejs.org/docs/latest-v26.x/api/typescript.html(opens in a new tab)
- How to set up TypeScripttypescriptlang.org/download/(opens in a new tab)