JavaScript in TypeScript

TypeScript provides compile time (not runtime) types safety for JavaScript code

Ts is intentionally and strictly a superset of JavaScript with optional type checks which means you can rename your .js file to a .ts file and typescript will still give you back a valid .js file equivalent to the original JavaScript file.

Types can be Implicit

typescript will try to infer as much of the types as it can.

var foo = 123;
foo = '456'; // Error: cannot assign `string` to `number`

// Is foo a number or a string?

TypeScript knows that the type of the variable “foo” is number with your given value.

Types can be Explicit

You can use annotation to refer to a specific type. We often do that for two reasons:

  1. Help your coworkers better understand your code by providing the a document (your code wirte in typesctip).
  2. enforce that what the compiler sees is what you thought it should see.

postfix type annotation

TypeScript uses postfix type annotation.

const foo: number = 123;

Types are structual

Ts provides type safety for JavaScript developers with a minimum cognitive overload.

Duck Typing is a first class language construct.