Slide 1

Slide 1 text

4 STEPS FROM JAVASCRIPT TO TYPESCRIPT @philnash

Slide 2

Slide 2 text

Phil Nash @philnash https://philna.sh [email protected]

Slide 3

Slide 3 text

4 STEPS FROM JAVASCRIPT TO TYPESCRIPT @philnash

Slide 4

Slide 4 text

I 💖 JAVASCRIPT @philnash

Slide 5

Slide 5 text

WHAT ABOUT TYPESCRIPT? @philnash

Slide 6

Slide 6 text

Modern JavaScript development •  Bigger projects •  Bigger teams •  More tools @philnash

Slide 7

Slide 7 text

Reasons to move to TypeScript •  Type safety •  Tooling •  Refactoring •  Confidence @philnash

Slide 8

Slide 8 text

Reasons not to move to TypeScript •  Learning curve •  Overhead •  "You don't need tests" •  New JS features @philnash

Slide 9

Slide 9 text

IT'S UP TO YOU AND YOUR TEAM @philnash

Slide 10

Slide 10 text

MOVE TO TYPESCRIPT @philnash

Slide 11

Slide 11 text

INTRODUCING THE DEMO APPLICATION @philnash

Slide 12

Slide 12 text

STEP 1: PAIR PROGRAM WITH TYPESCRIPT @philnash

Slide 13

Slide 13 text

I 💖 VSCODE @philnash

Slide 14

Slide 14 text

TYPESCRIPT IS BUILT IN @philnash

Slide 15

Slide 15 text

DEMO @philnash

Slide 16

Slide 16 text

Downsides •  It's only in the editor •  Native in VSCode and Atom •  TypeScript comments in a JS codebase @philnash

Slide 17

Slide 17 text

RECOMMENDATION: TURN IT ON GLOBALLY IN YOUR EDITOR @philnash

Slide 18

Slide 18 text

STEP 2: GET STARTED WITH THE TYPESCRIPT COMPILER @philnash

Slide 19

Slide 19 text

Time to install TypeScript npm install typescript --save-dev @philnash

Slide 20

Slide 20 text

Initialise the project npx tsc --init @philnash

Slide 21

Slide 21 text

tsconfig.json { "compilerOptions": { "target": "es5", "module": "commonjs", "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true } } 01. 02. 03. 04. 05. 06. 07. 08. 09. @philnash

Slide 22

Slide 22 text

tsconfig.json { "compilerOptions": { "target": "esnext", "module": "commonjs", "allowJS": true, "checkJS": false, "outDir": "./dist", "rootDir": "./src", "strict": false, ... } } 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. @philnash

Slide 23

Slide 23 text

Update package.json { "main": "dist/index.js", "scripts": { "build": "tsc", "watch": "tsc --watch" } } 01. 02. 03. 04. 05. 06. 07. @philnash

Slide 24

Slide 24 text

DEMO @philnash

Slide 25

Slide 25 text

STEP 3: CHECK JAVASCRIPT WITH TYPESCRIPT @philnash

Slide 26

Slide 26 text

tsconfig.json { "compilerOptions": { "target": "esnext", "module": "commonjs", "allowJS": true, "checkJS": true, ... } } 01. 02. 03. 04. 05. 06. 07. 08. 09. @philnash

Slide 27

Slide 27 text

Types for Node.js npm install @types/node --save-dev @philnash

Slide 28

Slide 28 text

DEFINITELY TYPED @philnash

Slide 29

Slide 29 text

DEMO @philnash

Slide 30

Slide 30 text

STOP THERE? @philnash

Slide 31

Slide 31 text

Compiling JavaScript with TypeScript •  TypeScript is type checking the JavaScript •  You can add further type annotations with JSDoc •  You don't need to output anything @philnash

Slide 32

Slide 32 text

BE LIKE WEBPACK @philnash

Slide 33

Slide 33 text

tsconfig.json { "compilerOptions": { // "outDir": "./dist", "noEmit": true, ... } } 01. 02. 03. 04. 05. 06. 07. @philnash

Slide 34

Slide 34 text

JSDOC: TYPESCRIPT WITHOUT WRITING TYPESCRIPT @philnash

Slide 35

Slide 35 text

JSDoc /** * @type string */ let foo; 01. 02. 03. 04. @philnash

Slide 36

Slide 36 text

JSDoc /** * @param {number} a * @param {number} b * @returns {number} */ function add(a, b) { return a + b; } 01. 02. 03. 04. 05. 06. @philnash

Slide 37

Slide 37 text

DEMO @philnash

Slide 38

Slide 38 text

STEP 4: MOVING TO TYPESCRIPT @philnash

Slide 39

Slide 39 text

🎉 FINALLY 🎉 @philnash

Slide 40

Slide 40 text

.js => .ts @philnash

Slide 41

Slide 41 text

DEMO @philnash

Slide 42

Slide 42 text

STRICT @philnash

Slide 43

Slide 43 text

Strict tsconfig options •  noImplicitAny •  noImplicitThis •  strictBindCallApply •  strictNullChecks •  strictFunctionTypes •  strictPropertyInitialization •  alwaysStrict @philnash

Slide 44

Slide 44 text

Strict tsconfig options •  strict @philnash

Slide 45

Slide 45 text

DEMO @philnash

Slide 46

Slide 46 text

FULL STRICT MODE IS NOT REQUIRED @philnash

Slide 47

Slide 47 text

BUT IT CAN BE A GOAL @philnash

Slide 48

Slide 48 text

MORE TYPES @philnash

Slide 49

Slide 49 text

DEMO @philnash

Slide 50

Slide 50 text

LIBRARY DEVELOPMENT @philnash

Slide 51

Slide 51 text

Library development { "compilerOptions": { "declaration": true, ... } } 01. 02. 03. 04. 05. 06. @philnash

Slide 52

Slide 52 text

WE MADE IT! @philnash

Slide 53

Slide 53 text

STEP 1: PAIR PROGRAM WITH TYPESCRIPT @philnash

Slide 54

Slide 54 text

STEP 2: GET STARTED WITH THE TYPESCRIPT COMPILER @philnash

Slide 55

Slide 55 text

STEP 3: CHECK JAVASCRIPT WITH TYPESCRIPT @philnash

Slide 56

Slide 56 text

STEP 4: MOVE TO TYPESCRIPT @philnash

Slide 57

Slide 57 text

Resources The demo repo https://github.com/philnash/four-steps-from-javascript-to-typescript @philnash

Slide 58

Slide 58 text

Resources Move your project to TypeScript at your own pace Dominik Kundel — https://www.twilio.com/blog/move-to-typescript TypeScript without TypeScript — JSDoc Superpowers Stefan Baumgartner — https://fettblog.eu/typescript-jsdoc-superpowers/ JS Projects Utilizing TypeScript TypeScript docs — https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html @philnash

Slide 59

Slide 59 text

Other links Definitely Typed: The Movie John Reilly — https://blog.johnnyreilly.com/2019/10/definitely-typed-movie.html The TypeScript Tax Eric Elliott — https://medium.com/javascript-scene/the-typescript-tax-132ff4cb175b @philnash

Slide 60

Slide 60 text

Thanks! @philnash https://philna.sh [email protected]