CPCODELAB
TypeScript

tsconfig Strictness Flags

9 min read

tsconfig Strictness Flags

The "strict": true shorthand in tsconfig.json is a family of individual flags. Knowing what each flag does helps you understand the errors you see — and lets you adopt them incrementally in a legacy codebase.

  • strictNullChecksnull and undefined are not assignable to other types; you must handle them explicitly.
  • noImplicitAny — raises an error when TypeScript would infer any for a parameter or variable with no annotation.
  • strictFunctionTypes — checks function parameter types contravariantly (prevents unsafe callbacks).
  • strictBindCallApply — ensures bind, call, and apply are typed correctly.
  • strictPropertyInitialization — class properties must be assigned in the constructor or have a definite-assignment assertion (!).
  • noImplicitThis — raises an error when this would be typed as any inside a function.
  • alwaysStrict — emits "use strict" in every output file and parses files in strict mode.
  • useUnknownInCatchVariables — catch clause variables are typed unknown instead of any (TypeScript 4.4+).
ts
// tsconfig.json — enabling flags individually
// {
//   "compilerOptions": {
//     "strictNullChecks": true,
//     "noImplicitAny": true,
//     "strictFunctionTypes": true,
//     "useUnknownInCatchVariables": true
//   }
// }

// With strictNullChecks:
function getLength(s: string | null): number {
  // return s.length; // Error — s could be null
  return s?.length ?? 0; // OK
}

// With useUnknownInCatchVariables:
try {
  JSON.parse("{");
} catch (err) {
  // err is unknown — must narrow before use
  if (err instanceof Error) {
    console.error(err.message);
  }
}

Additional safety flags

  • noUncheckedIndexedAccess — accessing arr[i] or obj[key] returns T | undefined, not just T.
  • exactOptionalPropertyTypes — optional properties may not be explicitly set to undefined; they must be omitted.
  • noImplicitReturns — every code path in a function must return a value if the return type is not void.
  • noFallthroughCasesInSwitch — switch cases without break or return are flagged.
  • noPropertyAccessFromIndexSignature — dot notation is disallowed on index-signature types; bracket notation is required.

For new projects: turn on "strict": true plus noUncheckedIndexedAccess and exactOptionalPropertyTypes from day one. For migrations: enable flags one at a time, fix errors, commit, repeat — noImplicitAny first, then strictNullChecks.

Ready to test yourself?
Practice TypeScript— quiz & coding exercises