Typescript
up:: JavaScript
Ressources
Why Typescript
- Type checking
- Code completion
- Additional features
When to use TS
- Just use enough Types to be helpful
- Think about types more as documentation rather than โtypescript all the thingsโ
- What is good about Typescript is that it adds auto-completion
Types
- Typescript any
- TypeScript Tuples
- TypeScript enum
- Typescript objects
- TypeScript Union Types TypeScript Functions
Function
Type alias
Namespaces
global.d.ts
is a global namespace
Defining type
Interfaces
Interfaces are basically type definitions for objects.
In order to make an interface โloop-ableโ, we need to specify the key:
Type
Defining a type
looks a bit like defining a variable with let
. By convention, they are UpperCamelCased.
Prefer interfaces
According to the documentation, it is best practice to prefer using interfaces and only use type when specific features are required.
Type Erasure
The types are only checked while compiling and then โthrown awayโ. Typescript checks all the type-specific syntax and then compiles into pure JavaScript.
Letโs take a line like this:
All the Typescript syntax gets evaluated and then erased. That leaves zero JavaScript to be compiled. When running the program, it returns undefined
.
Summary
JavaScript: syntax check โ execution. TypeScript: syntax check โ type check โ execution.
Get the return type of a function
This is handy when initialising a variable.