Full-stack Web Technologies

CHAPTER 2
Typescript

Typescript is a layer on top of Javascript which provides a very rich and powerful type system. Typescript is not independent of Javascript. It just augments it with some syntax but its compiler just outputs regular Javascript that can be executed on a browser or with a runtime like NodeJS.

Typescript's type system helps in at least 3 important ways:

  • Catches programming errors before execution (by checking that types match).
  • Helps editors show errors while typing.
  • Shows possible completions for fields, methods and parameters.

Productivity increases because less bugs are introduced while programming and it is less costly to ship correct software.

Bun

Bun is a Javascript and Typescript runtime which tries to be as fast as possible and includes Typescript inside. This is very convenient for Typescript beginners, since it doesn't need any configuration at all to start writing programs.

To install Bun, issue this command on the terminal:

curl -fsSL https://bun.sh/install | bash

If you are on Windows, first install the Windows Subystem for Linux (or WSL), which runs Linux binaries on top of the Windows OS kernel.

Typescript Configuration

The tsconfig.json file resides at the root of a Typescript project (usually alongside package.json) and controls the Typescript compiler options.

The compilerOptions section
module"CommonJS", "ESNext", ...
sourceMaptrue or false
stricttrue or false, stronger guarantees of program correctness
Included files

In the files section you can list the files which are part of the program. It is an error that any of those are missing.

{
  "files": ["main.ts", "utils.ts", "load.ts"]
}

For big programs, it is easier to use glob patterns to match files in the include section:

{
  "include": ["src/**/*.ts", "tests/**/*.ts"]
}