Slide 1

Slide 1 text

Mug in Clermont | LavaJUG | Clermont'ech, March 2019

Slide 2

Slide 2 text

Premier Field Engineer @spontoreau In ❤️ with Co-organizer

Slide 3

Slide 3 text

@ParisTypeScript

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

https://github.com/Microsoft/TypeScript

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

transpile ts js $ tsc config

Slide 8

Slide 8 text

Editors tsserver server.ts Language Service service.ts TS standalone compiler tsc.ts Core TypeScript Compiler core.ts, emitter.ts, checker.ts, parser.ts, …

Slide 9

Slide 9 text

Slide 10

Slide 10 text

{ "name": "angular-with-dts", "version": "1.0.0", "dependencies": { "angular": "1.5.8" }, "devDependencies": { "@types/angular":"1.5.20" } } $ npm install --save-dev @types/angular package.json:

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

let str: string; let array: number[] = [1, 2, 3]; function fn(param: boolean): void { // Do something }

Slide 14

Slide 14 text

interface Rocket { boosters: number; launch: () => void; } class Soyuz implements Rocket { boosters : number; constructor() { this.boosters = 4; } launch(): void { … }; } let soyuzT15: Rocket = new Soyuz(); let soyuzMS3: Soyuz = new Soyuz(); Class or Interface… How to choose?

Slide 15

Slide 15 text

any

Slide 16

Slide 16 text

class Rocket { name: string; boosters: number; } class Cat { name: string; } function playWithKitten(kitten: Cat) { // Do something } const rocket: Rocket = { name: "Soyuz", boosters: 4 }; const cat: Cat = { name: "Grumpy" }; playWithKitten(cat); playWithKitten(rocket); // ☺

Slide 17

Slide 17 text

• TypeScript is JavaScript. • Interface is only a TypeScript compiler concept (type checking). • Sometime you have to deal with the any type (Example: third part). • The Duck example: const duck = { fly: () => console.log("I can fly!"), swim: () => console.log("I can swin!"), feather: true }; const penguin = { swim: () => console.log("I can swin!"), feather: true }; const isDuck = (obj: any) => (obj.hasOwnProperty("feather") && obj.fly && obj.swim); if(isDuck(duck)) { (duck).fly(); } if(!isDuck(penguin)) { console.log("Penguin can't fly...") }

Slide 18

Slide 18 text

interface Rocket { boosters: number; launch?: () => void; } Read-only: interface Rocket { readonly boosters: number; launch?: () => void; } interface Launch { () => void; } Function: Cannot be implemented by a class !

Slide 19

Slide 19 text

abstract class Rocket { boosters: string; abstract launch(): void; } class Soyuz extends Rocket { } class Soyuz extends Rocket { launch(): void { throw new Error("Not implemented."); } }

Slide 20

Slide 20 text

abstract class Rocket { readonly boosters: number; constructor(boosters: number) { this.boosters = boosters; } abstract launch() : void; } class RocketFactory { ctor: new (boosters: number) => T; constructor(ctor: new (boosters: number) => T) { this.ctor = ctor; } create(boosters: number): T { return new this.ctor(boosters); } }

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Types === static constraints • Type categories: • primitive • object • union • intersection • Special type: • any • null • undefined • void • Type alias type Bool = boolean;

Slide 23

Slide 23 text

The light version ☺ undefined null void number primitive string boolean any object array class interface function

Slide 24

Slide 24 text

Discriminated Union Intersection Union Type Guards Mapped Conditional Infer Immutability

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content