TypeScript
TypeScript is a programming language built on top of JavaScript that adds static types. It catches type-related mistakes while you write code, then compiles down to plain JavaScript that runs anywhere JS does.
TypeScript
TypeScript is a 'superset' of JavaScript: every valid JavaScript file is already valid TypeScript, so you can adopt it gradually. The extra type information is checked by the compiler (tsc) and then erased — the browser or Node never sees the types.
The big difference from plain JavaScript is that many bugs are caught at compile time instead of at runtime. Misspelled property names, passing a string where a number is expected, or calling a method that doesn't exist all become errors in your editor before you ever run the program.
Syntax
let count: number = 5;Examples
TypeScript
function greet(name: string): string {
return "Hi, " + name;
}
console.log(greet("Lee"));Output:
Hi, LeeTypeScript — Compile error: Type 'string' is not assignable to type 'number'.
let age: number = 25;
age = "old";