TypeScript

TypeScript is a superset of JavaScript that introduces static typing. Its growing popularity stems from the added type safety and improved tooling, which many developers find invaluable for managing large codebases.

It is reassuring when you make numerous adjustments in a sizable codebase and all type checks pass, confirming that you haven't unintentionally broken anything.

However, some developers criticize TypeScript for its initial complexity. The type system is quite sophisticated, which can be daunting for newcomers or those from different programming backgrounds. Features such as generics, type inference, and union/intersection types may feel overwhelming at first, but they offer substantial advantages once mastered.

Running TypeScript

TypeScript has become increasingly integrated into various tools and runtimes, making it more accessible for developers.

Deno and Bun support TypeScript natively, allowing you to run TypeScript files directly without additional setup. This is a significant advantage for rapid development and prototyping.

The latest version of Node.js has introduced the --experimental-strip-types flag, which simplifies running TypeScript files by stripping type annotations. However, this is mainly for execution purposes, while TypeScript's static type checking still requires the TypeScript compiler.

Using the TypeScript Compiler

To get started with TypeScript, install it and add it to package.json's devDependencies:

npm install --save-dev typescript

Next, add a script in your package.json:

"scripts": {
  "tsc": "tsc -p ."
}

Edit your tsconfig.json to configure the compiler options:

{
  "compilerOptions": {
    "target": "es6",               // Specify ECMAScript target version
    "module": "commonjs",          // Specify module code generation
    "strict": true,                // Enable all strict type-checking options
    "esModuleInterop": true        // Enable interoperability between CommonJS and ES Modules
  },
  "include": [
    "src/**/*.ts"                  // Specify which files to compile
  ]
}

Compile and perform type checking by running:

npm run tsc

Learning TypeScript may take some time, but it's well worth the investment. For comprehensive resources, visit the TypeScript documentation.