Codectionary / Developer documentation / TypeScript

Strict Mode Options

The "strict" tsconfig option is actually a shorthand that enables a whole family of individual strictness flags at once, including strictNullChecks (null/undefined are not assignable to other types by default), noImplicitAny (variables must have an inferable or explicit type), and strictFunctionTypes. Enabling strict mode is one of the highest-value changes for TypeScript's type safety.

Syntax

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

Examples

What strict: true Actually Enables

The individual flags bundled together by the strict shorthand.

{
  "compilerOptions": {
    "strict": true
    // equivalent to enabling all of:
    // "noImplicitAny": true,
    // "strictNullChecks": true,
    // "strictFunctionTypes": true,
    // "strictBindCallApply": true,
    // "strictPropertyInitialization": true,
    // "noImplicitThis": true,
    // "alwaysStrict": true
  }
}

strictNullChecks in Action

The single most impactful strict flag - catching a huge class of "cannot read property of undefined" bugs.

// Without strictNullChecks:
function getLength(str: string) {
  return str.length; // no warning, even though str could be null/undefined
}

// With strictNullChecks:
function getLength(str: string | null) {
  // return str.length; // Error - str could be null here
  return str?.length ?? 0; // forces you to actually handle the null case
}

Best practices

  • Enable strict mode from the very start of a new project - retrofitting it onto a large existing codebase later is significantly more work
  • If migrating an existing project, consider enabling individual strict flags one at a time (starting with strictNullChecks) rather than flipping strict all at once
  • Treat strictNullChecks errors as genuine bugs to fix, not obstacles - they almost always represent a real, previously-unhandled null/undefined case
  • Pair strict mode with noUncheckedIndexedAccess for even stronger guarantees around array/object index access returning possibly-undefined values

At a glance

Purpose
Static types for JavaScript
File extension
.ts ยท .tsx
Runs in
Compiled to JavaScript
Usually used with
JavaScript and its ecosystem

Specifications & further reading

Related TypeScript documentation