Introduction
TypeScript is a strongly typed programming language that builds on JavaScript. Every JavaScript program is also valid TypeScript; TypeScript adds type annotations on top, so mistakes are caught while you write code instead of when it runs.
It was created by Anders Hejlsberg at Microsoft and first released in October 2012.
How It Works
Browsers and Node.js run JavaScript, not TypeScript. The TypeScript compiler (tsc) does two jobs:
- Type checking: reads your
.tsfiles and reports type errors. - Emitting: removes the types and writes plain
.jsfiles.
app.ts ──► tsc ──► type errors (if any)
└► app.js ──► browser / Node.js
Types exist only at compile time. They are erased from the output, so they never slow down your program, and they can't be checked at runtime.
// app.ts
let message: string = "Hello, world";
// app.js (after tsc)
let message = "Hello, world";
Why TypeScript?
Errors Before Runtime
Typos, wrong argument types and missing properties are reported in your editor, before the code ever runs.
const user = { name: "Pirate Dev" };
user.nmae; // Error: Property 'nmae' does not exist on type '{ name: string; }'
Better Editor Support
Because the editor knows the type of every value, it can offer accurate autocompletion, inline documentation, "go to definition" and safe renames across the whole project.
Code That Documents Itself
Type annotations describe what a function expects and returns. They stay correct, because the compiler checks them - unlike comments.
Gradual Adoption
TypeScript can be added to an existing JavaScript project one file at a time. You can start with very few annotations and let TypeScript infer the rest.
