1 of 79

const name: Language = 'TypeScript';

@midwest_js

2 of 79

Abraham Williams

Pearl Latteier

Senior Developers at Bendyworks

Google Developer Experts

Organizers of GDG Madison

​

3 of 79

slides.today

4 of 79

Why?

5 of 79

TypeScript helps you write better JavaScript

TLDR:

6 of 79

Catch errors

7 of 79

Refactor confidently

8 of 79

Self-documenting

9 of 79

Modern JavaScript

10 of 79

What?

11 of 79

12 of 79

Development

Browser

13 of 79

TypeScript compiles to JavaScript

​

// app.ts

interface Person {

name: string;

}

​

const author: Person = {

name: 'Pearl',

}

​

​

// app.js

const author = {

name: 'Pearl',

};

​

14 of 79

Types

15 of 79

16 of 79

Type Annotation

​

const num1 = 123;�const num2 = 4;

17 of 79

Type Annotation

​

const num1: number = 123;�const num2: number = 4;

​

18 of 79

Type Annotation

​

​

const multiply = (a: number, b: number) => {� return a * b;�}

19 of 79

Type Annotation

​

​

const multiply = (a: number, b: number): number => {� return a * b;�}

20 of 79

Type Annotation

​

const multiply = (a: number, b: number) => {� return a * b;�}

​

multiply('1', 2);

21 of 79

WTF Syntax Quiz

let stuff: string[];

let things: Array<number>;

22 of 79

let dunno = ??;

23 of 79

Any

let dunno: any;

“opt-out of type-checking”

24 of 79

The problem with Any

const thing: any = 42;�thing('nope');

25 of 79

Don’t use 'any'

26 of 79

Interfaces

27 of 79

Interfaces

interface Person {� firstName: string;� lastName: string;� age?: {

amount: number;

unit: string;

}�}

28 of 79

Interfaces

const pearl: Person = {� firstName: "Pearl",� lastName: "Latteier"�};

29 of 79

Interfaces

const pearl: Person = {

firstName: "Pearl"

​

};

30 of 79

Interfaces

function logAge(user: Person) {

console.log(user.age.amount);

}

31 of 79

Duck typing

32 of 79

​

​

​

function render(

thing: { display: string }

): string => {� return thing.display;�}

Duck typing

33 of 79

Duck typing

​

​

​

interface Person {� display: string;�}

​

interface Book {� display: string;

pages: number;�}

​

34 of 79

Custom types

35 of 79

Custom types

type HasVowels = (arg: string) => boolean;

36 of 79

​

type HasVowels = (arg: string) => boolean;

​

interface HasVowels {

(arg: string): boolean

}

Interface vs Type

37 of 79

Custom types

type Thing = Person | Book;

38 of 79

Custom types compiled

39 of 79

Generics

40 of 79

Build utilities with dynamic types

41 of 79

function randomItem<T>(arr: T[]): T {

let randomIndex =

Math.floor(Math.random() * arr.length);

return arr[randomIndex];

}

Generics

Example from https://code.tutsplus.com/tutorials/typescript-for-beginners-part-5-generics--cms-29603

42 of 79

randomItem<string>(['once', 'twice']);

randomItem([1, 2, 3]);

Generics

Example from https://code.tutsplus.com/tutorials/typescript-for-beginners-part-5-generics--cms-29603

43 of 79

“T” is customary but anything works

44 of 79

Remote data

// lib.ts

type RemoteData<E, D> = Initialized | Pending | Failure<E> | Success<D>;

​

// app.ts

type State = RemoteData<number, Tweet>;

​

45 of 79

Remote data

// lib.ts

type RemoteData<E, D> = Initialized | Pending | Failure<E> | Success<D>;

​

// app.ts

type State = RemoteData<number, Tweet>;

​

46 of 79

Remote data

// lib.ts

type RemoteData<E, D> = Initialized | Pending | Failure<E> | Success<D>;

​

// app.ts

type State = RemoteData<number, Tweet>;

​

47 of 79

Remote data

// lib.ts

type RemoteData<E, D> = Initialized | Pending | Failure<E> | Success<D>;

​

// app.ts

type State = RemoteData<number, Tweet>;

​

48 of 79

Remote data

https://bendyworks.com/blog/slaying-a-ui-antipattern-with-web-components-and-typescript

49 of 79

WTF Syntax Quiz

function echo<T>(arg: T): T {� return arg;�}

50 of 79

51 of 79

How?

52 of 79

cd your-project-dir

53 of 79

npm install --save-dev typescript

54 of 79

$ npx tsc --init

55 of 79

tsconfig.json

56 of 79

// tsconfig.json with defaults�{� "compilerOptions: {

"target": "es5", � "module": "commonjs", � "strict": true, � "esModuleInterop": true

}

}

57 of 79

"target"

Output JS version

ES5

ES2016

ESNext

Etc

58 of 79

"module"

Output module type

commonjs

umd

ESNext

Etc

59 of 79

"strict"

Enable all strict type-checking options

true

false

60 of 79

Use 'strict'

61 of 79

$ mv app.js app.ts

62 of 79

$ npx tsc

63 of 79

app.ts:4:10 - error TS2345: Argument of type '"a"' is not assignable to parameter of type 'number'.

64 of 79

Add types!

65 of 79

What about JS libraries?

66 of 79

DefinitelyTyped

$ npm i --save-dev @types/lodash

67 of 79

Tooling

68 of 79

tslint

$ npm install --save-dev tslint

$ npx tslint --init

$ npx tslint --project tsconfig.json

69 of 79

TypeDoc

/**

* Renders a template to a container.

*

* To update a container with new values, reevaluate the template literal and call `render` with the new result.

*

* @param result a TemplateResult created by evaluating a template tag like `html` or `svg`.

* ...

*/

​

​

70 of 79

71 of 79

Visual Studio Code

72 of 79

$ npm install ts-node

ts-node

73 of 79

ts-node

$ npx tsc app.ts

$ node app.js

$ npx ts-node app.ts

74 of 79

75 of 79

Narrowing the Field - Choosing the Right Compile-to-JS Language for You

Schulze 127 @ 2:30 with @_rtwalsh

76 of 79

Wicked Fast ⚡ Mobile Web

Schulze 127 @ 1:30 Friday

77 of 79

Resources

slides.today

78 of 79

We design and build software that stands the test of time, specializing in Ruby on Rails, JavaScript, Clojure, and Ionic.

​

Craft a project with us at bendyworks.com

79 of 79

Find today’s slides at slides.today

Pearl Latteier

pearl@bendyworks.com

twitter.com/pblatteier

github.com/pearlbea

Abraham Williams

abraham@bendyworks.com

twitter.com/abraham

github.com/abraham

​