TypeScript is a typed superset of JavaScript, which means that all JavaScript code is valid TypeScript code. TypeScript adds a lot of new features on top of that.

TypeScript makes JavaScript more like a strongly-typed, object-oriented language akin to C# and Java. This means that TypeScript code tends to be easier to use for large projects and that code tends to be easier to understand and maintain. The strong typing also means that the language can (and is) precompiled and that variables cannot be assigned values that are out of their declared range. For instance, when a TypeScript variable is declared as a number, you cannot assign a text value to it.

This strong typing and object orientation makes TypeScript easier to debug and maintain, and those were two of the weakest points of standard JavaScript.

Type declarations

You can add type declarations to variables, function parameters and function return types. The type is written after a colon following the variable name, like this: var num: number = 5; The compiler will then check the types (where possible) during compilation and report type errors.

var num: number = 5;
num = "this is a string";  // error: Type 'string' is not assignable to type 'number'.

The basic types are :

- `number[]` - array of numbers
- `Array<string>` - array of strings
- `[boolean, string]` - tuple where the first element is a boolean and the second is a string.
- `[number, number, number]` - tuple of three numbers.
- `{name: string, age: number}` - object with name and age attributes
- `{[key: string]: number}` - a dictionary of numbers indexed by string
- `(param: number) => string` - function taking one number parameter returning string
- `() => number` - function with no parameters returning an number.
- `(a: string, b?: boolean) => void` - function taking a string and optionally a boolean with no return value.