TypeScript

https://www.typescriptlang.org/

Notes

  • Typed superset of JavaScript developed by Microsoft

  • It compiles to readable JavaScript

  • The type system disappears during the build step, so there is no runtime penalty

  • By moving some errors from runtime to compile time, it becomes easier and faster to fix them

Configuring TypeScript

  • Flags to the tsc command

tsc src/index.ts --target ES2017 --module commonjs --watch
  • tsconfig.json file

{
    "compilerOptions": {
        "jsx": "react", // transform TSX files into JSX
        "strict": true, // enable strict features
        "module": "commonjs",
        "allowJs": true, // check and compile JS
        "target": "es2017", // target environment
        "outDir": "lib",
        "declaration": true, // create *.d.ts files
        "sourceMap": true, // create source map for debug
    },
    "include": ["src"], // input files
}

Basics

Functions

Interfaces and Type Aliases

Classes

Converting from JavaScript to TypeScript

  1. Compile project in "loose mode"

    1. start with tests passing

    2. rename all files to .ts, with implicit any

    3. fix only compile errors (do not change behaviour)

    4. get tests passing again

  2. Explicit Any

    1. start with tests passing

    2. add "noImplicitAny": true to tsconfig.json

    3. Where possible, provide a specific and appropriate type (explicit any otherwise)

    4. get tests passing again

  3. Squash explicit anys, enable strict mode

    1. Enable strict mode in tsconfig.json

    2. Replace explicit anys with more appropriate types (incrementally, in small chunks)

Generics

Top and Bottom types

Advanced Types

Resources

Books

Courses

GitHub Repositories

  • TypeStat - Converts JavaScript to TypeScript and TypeScript to better TypeScript

Videos

Websites

Last updated

Was this helpful?