Slide 1

Slide 1 text

Typescript The Better JavaScript? Christian Köberl / / github.com/derkoe @derkoe

Slide 2

Slide 2 text

Christian Köberl Software Architect / Developer Develops webapps since 1999

Slide 3

Slide 3 text

26 countries, 160 products, 450 employees More than 1M vehicles sold per year Datacenter 1500 hosts, 60 TB RAM, 1500 TB disk Powering Mobility Business for a Smart World

Slide 4

Slide 4 text

Why TypeScript?

Slide 5

Slide 5 text

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Code Completion aka Intellisense

Slide 8

Slide 8 text

Refactoring Car → Vehicle

Slide 9

Slide 9 text

Backend APIs → TS

Slide 10

Slide 10 text

Felix Rieseberg, »How I Learned to Stop Worrying & Trust the Compiler« TypeScript at Slack

Slide 11

Slide 11 text

Using Future ECMAScript Features async/await Decorators (@)

Slide 12

Slide 12 text

ES6 Classes Types? class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } } let person = new Person(1, 2); person.mama = 'Mia'; person = Math.sqrt(person);

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

TypeScript TypeScript ES6 / ES2016 ES5 Statically-typed superset of ES5 and ES2015/16 Developed by Anders Hejlsberg / Microsoft 2012

Slide 15

Slide 15 text

Typescript vs ES6 vs ES5 export class Person { private dateOfBirth?: Date; constructor(private firstName: string, private lastName: string) { } toString() { return `${this.firstName} ${this.lastName} (${this.dateOfBirth})`; } }

Slide 16

Slide 16 text

Type Declarations let a: number; function toLowerCase(a: string|null) { return a ? a.toLowerCase() : null; } type AorB = 'A'|'B'; const x: AorB = 'A'; type BiFunc = (a: number, b: number) => number;

Slide 17

Slide 17 text

Interfaces interface Person { firstName: string; lastName: string; dateOfBirth?: Date; } class Employee implements Person { constructor(public firstName: string, public lastName: string) { } }

Slide 18

Slide 18 text

Type Compatibility Structural Typing aka Duck Typing const p = { firstName: 'Chris', lastName: 'Kö' }; const person: Person = p; person.dateOfBirth = new Date();

Slide 19

Slide 19 text

Type Inference const name = 'Christian'; // no type needed // even no types for complex stuff document .getElementById('id') .addEventListener('click', ev => alert(`${ev.clientX}, ${ev.clientY}`));

Slide 20

Slide 20 text

Generic Types class Stack { private items: T[] = []; public push(item: T) { this.items.push(item); } public pop(): T { return this.items.pop(); } } const stack = new Stack(); stack.push("a"); const a = stack.pop(); // a is a string

Slide 21

Slide 21 text

Integrating JavaScript TypeScript Declaration Files - d.ts Just types, declarations and interfaces / DefinitelyType Typings

Slide 22

Slide 22 text

Modules and Namespaces Modules correspond to ES6 modules export and import Namespaces are translated to IIFEs module pattern

Slide 23

Slide 23 text

Advanced Topics

Slide 24

Slide 24 text

Decorators Access control, Validation Logging, Profiling Frameworks (e.g. Angular) @Component({a = '1'}) // <<-- decorator class MyClass { @measure // <<-- decorator public doSomething() { // ... } }

Slide 25

Slide 25 text

Creating Decorators function measure(target: any, propertyKey: string, descriptor: PropertyDescriptor) { const originalFunction = descriptor.value; descriptor.value = function (...args: any[]) { const start = Date.now(); originalFunction.apply(target, args); console.log(`${propertyKey} with params "${args}" took ${Date.now() - start}ms`); } }

Slide 26

Slide 26 text

async/await since Typescript 2.3 for target ES5 function loadUserName(userId) { const url = `https://user-api/user/${userId}`; return fetch(url) .then(response => response.json()) .then(user => user.name); } async function loadUserName2(userId: string) { const url = `https://user-api/user/${userId}`; const response = await fetch(url); const user = await response.json(); return user.name; }

Slide 27

Slide 27 text

elm Similar Tools Flow is a static type checker for JavaScript. A transpiler from Java to TypeScript/JavaScript A delightful language for reliable webapps.

Slide 28

Slide 28 text

Summary Static type checking is awesome! (especially with TypeScript) Use ES2015 and ES.next features!

Slide 29

Slide 29 text

Thanks! Questions? PS: We are hiring!

Slide 30

Slide 30 text

Image Credits Title Christian Köberl: © Christian Köberl All others © Porsche Informatik Rotating arms: Imgur

Slide 31

Slide 31 text

References, Further Readings Bowden Kelly: TypeScript The Language You Already Know TypeScript Weekly Oliver Zeigermann: Typed JavaScript using TypeScript and Flow