Typescript Foundations / Typescript Tooling

Strict mode options

✅ strict (master switch)

{
  "compilerOptions": {
    "strict": true
  }
}

Setting "strict": true enables all strict type-checking options. It's shorthand for turning on all of the following:


strictNullChecks

Ensures values like null and undefined must be explicitly handled.

let name: string = null; // ❌ Error
let name: string | null = null; // ✅

noImplicitAny

Reports an error when TypeScript infers the any type implicitly.

function greet(name) { // ❌ Error: name is implicitly any
  console.log("Hello", name);
}

strictFunctionTypes

Performs stricter checks on function parameter types (ensures safer function assignments).

type A = (x: number) => void;
type B = (x: number | string) => void;

let fn: A = (x) => console.log(x);
let fn2: B = fn; // ❌ Error

strictBindCallApply

Checks argument types for .bind, .call, and .apply methods on functions.

function add(a: number, b: number) { return a + b; }
add.bind(null, '1'); // ❌ Error: string is not assignable to number

strictPropertyInitialization

Ensures class properties are initialized in the constructor (unless marked optional or with definite assignment !).

class Person {
  name: string; // ❌ Error: not initialized
  constructor() {
    this.name = "John"; // ✅
  }
}

noImplicitThis

Gives an error if this has an implicit any type.

function sayHi() {
  console.log(this.name); // ❌ Error
}

function sayHi(this: { name: string }) { // ✅
  console.log(this.name); 
}

alwaysStrict

Adds "use strict" to all output JS files and parses files in strict mode.

"use strict";

useUnknownInCatchVariables

Ensures that caught errors are typed as unknown instead of any.

try {
  // ...
} catch (err) {
  console.log(err.message); // ❌ Error: 'message' does not exist on unknown
}

catch (err: unknown) { // ✅
  if (err instanceof Error) {
    console.log(err.message);
  }
}

🧩 Example tsconfig.json with all strict options

{
  "compilerOptions": {
    "strict": true,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "useUnknownInCatchVariables": true
  }
}