4 Steps from JavaScript
to TypeScript

Phil Nash

Phil Nash
Developer Advocate for Sonar

Sonar
Logos

Phil Nash

twitter.com/philnash

@philnash@mastodon.social

linkedin.com/in/philnash

https://philna.sh

Phil Nash

Why move to TypeScript?

Type Safety

Tooling

Confidence

Why not move to TypeScript?

  • Learning curve
  • Overhead
  • "You don't need tests"
  • New JS features

It's up to you and your team

Let's move to TypeScript

Introducing the demo application

Step 1

Pair program with TypeScript

Demo

Recommendation

Turn on TypeScript checking globally

Step 2

Introduce the TypeScript compiler

Install Typescript

npm install -D typescript

Initialise your project

npx tsc --init

tsconfig.json

{
  "target": "es2016",
  "module": "commonjs",
  "strict": true,
  "esModuleInterop": true,
  "skipLibCheck": true,
  "forceConsistentCasingInFileNames": true
}

Update tsconfig.json

  {
-   "target": "es2016",
+   "target": "esnext",
    "module": "commonjs",
-   "strict": true,
+   "strict": false,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
+   "allowJS": true,
+   "checkJS": false,
+   "outDir": "./dist",
+   "rootDir": "./src"
  }

Update package.json

  {
-   "main": "./src/cli.js",
+   "main": "./dist/cli.js",
    "scripts": {
+     "build": "tsc",
+     "watch": "tsc --watch"
    },
  }

Demo

Step 3

Check JavaScript with TypeScript

Update tsconfig.json again

  {
    "target": "esnext",
    "module": "commonjs",
    "strict": false,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "allowJS": true,
-   "checkJS": false,
+   "checkJS": true,
    "outDir": "./dist",
    "rootDir": "./src"
  }

Demo

Stop there?

Compiling JS with TS

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

Webpack and Svelte

TypeScript checks without output

  {
    "compilerOptions": {
+//   "outDir": "./dist",
+     "noEmit": true,
      ...
    }
  }

JSDoc

TypeScript without writing TypeScript

JSDoc

/**
 * @type string
 */
let searchTerm;
          

JSDoc

/**
 * @param {number} a
 * @param {number} b
 * @returns {number}
 */
function add(a, b) {
  return a + b;
}

Demo

Step 4

Move to TypeScript

Incrementally

Change .js to .ts

Change from require to import

Demo

Incrementally

Add types

Enable strict flags

We made it!

Step 1

Pair program with TypeScript

Step 2

Introduce the TypeScript compiler

Step 3

Check JavaScript with TypeScript

Step 4

Move to TypeScript

Resources

The demo repo
https://github.com/philnash/dad-jokes-cli

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

Resources

QR code for the slides

Thank you

twitter.com/philnash

@philnash@mastodon.social

linkedin.com/in/philnash

https://philna.sh

QR code for the slides
Phil Nash Sonar