Slide 1

Slide 1 text

Advanced Typing in TypeScript WEAREDEVELOPERS WORLD CONGRESS / 15.06.2022 LARS HUPEL @larsr_h · they/them

Slide 2

Slide 2 text

https://www.typescriptlang.org/

Slide 3

Slide 3 text

Core tenets • • • • • JavaScript + X Environment independence Structural typing (aka "duck typing") Type inference Support of wonky JavaScript patterns

Slide 4

Slide 4 text

What is JS? Chapter 1

Slide 5

Slide 5 text

JS does not stop you from … • • • adding arrays to objects multiplying objects with numbers comparing 🍎 with 🍊

Slide 6

Slide 6 text

TypeScript set out to fix this. … and fixing they did

Slide 7

Slide 7 text

What JS devs asked for What JS devs got

Slide 8

Slide 8 text

“Types are a sign of defeat. You lost control of your life and then you just use types to program JavaScript.”

Slide 9

Slide 9 text

Typing the DOM Chapter 2

Slide 10

Slide 10 text

addEventListener(type, listener); addEventListener(type, listener, options); addEventListener(type, listener, useCapture);

Slide 11

Slide 11 text

button.addEventListener('click', event => { console.log(`Click count: ${event.detail}`); });

Slide 12

Slide 12 text

button.addEventListener('click', event => { console.log(`Click count: ${event.detail}`); }); Event MouseEvent 🔍 UIEvent

Slide 13

Slide 13 text

interface GlobalEventHandlersEventMap { "abort": UIEvent; "animationcancel": AnimationEvent; // ... "click": MouseEvent; // ... } interface GlobalEventHandlers { addEventListener( type: K, listener: (ev: GlobalEventHandlersEventMap[K]) => any, options?: boolean | AddEventListenerOptions ): void; }

Slide 14

Slide 14 text

interface GlobalEventHandlersEventMap { "abort": UIEvent; "animationcancel": AnimationEvent; // ... "click": MouseEvent; // ... } interface GlobalEventHandlers { addEventListener( type: K, listener: (ev: GlobalEventHandlersEventMap[K]) => any, options?: boolean | AddEventListenerOptions ): void; } 🔍 🔍 "abort" | "animationcancel" | "click" | /* … */ GlobalEventHandlersEventMap["click"]

Slide 15

Slide 15 text

same thing for Node …

Slide 16

Slide 16 text

Dataframes! 🐼 Chapter 3

Slide 17

Slide 17 text

Data analysis • • • Data Science™ is basically just tables, right? CSV is a great format for tables … but what do we do with them?

Slide 18

Slide 18 text

Typical operations • • • • • access row i at column c filter rows by predicate drop columns add new column aggregate & group by column

Slide 19

Slide 19 text

operation(table: Table): Table

Slide 20

Slide 20 text

🔍 operation(table: Table): Table

Slide 21

Slide 21 text

operation(table: Table): Table

Slide 22

Slide 22 text

🔍 operation(table: Table): Table

Slide 23

Slide 23 text

operation(table: Table): Table 🔍 interface Table< Cols extends Record >

Slide 24

Slide 24 text

# Order ID Item Qty Price 1 ABC Apple 5 10 2 ABC Pear 2 3 3 DEF Apple 1 8

Slide 25

Slide 25 text

# Order ID Item Qty Price 1 ABC Apple 5 10 2 ABC Pear 2 3 3 DEF Apple 1 8 🔍 Table<{ orderId: string; item: string; qty: number; price: number; }>

Slide 26

Slide 26 text

Implementation Chapter 4

Slide 27

Slide 27 text

interface Series { get length(): number; at(n: number): Elem; }

Slide 28

Slide 28 text

interface Table> { get index(): Series select(col: Col): Series lookup(index: Index): Row | undefined }

Slide 29

Slide 29 text

Demo

Slide 30

Slide 30 text

innoq.com/wad-typescript

Slide 31

Slide 31 text

Q&A Lars Hupel (they/them) [email protected] @larsr_h www.innoq.com innoQ Deutschland GmbH Krischerstr. 100 40789 Monheim +49 2173 333660 Ohlauer Str. 43 10999 Berlin Ludwigstr. 180E 63067 Offenbach Kreuzstr. 16 80331 München Hermannstr. 13 20095 Hamburg Erftstr. 15-17 50672 Köln Königstorgraben 11 90402 Nürnberg

Slide 32

Slide 32 text

Images • • Karl Lagerfeld by Adach, CC-BY-SA 2.0, https://commons.wikimedia.org/w/index.php? title=File:Karl_Lagerfeld_(14091153382)_(cropped).jpg&oldid=477165629 Mechanical adding machine by Nxr-at, CC-BY-SA 4.0, https://commons.wikimedia.org/w/index.php? title=File:Mechanical_adding_machines_WW_Continental.JPG&oldid=470727 928