CodePark/ questions / Modules, .d.ts & tsconfig
START PRACTICINGPRACTICE
Frontend question · TypeScript › Modules, .d.ts & tsconfig

Which tsconfig strictness options matter most, and what does each one catch?

Turn on strict: true first. It enables a group of checks, and new projects should always use it.

The most important checks inside strict:

  • strictNullChecks keeps null and undefined out of every type unless you add them. So you must handle missing values. Example: document.getElementById("x").focus() errors, because the element may be null.
  • noImplicitAny gives an error when TypeScript cannot infer a type and would silently use any. Example: function f(x) {} errors until you type x.
  • strictFunctionTypes checks function parameter types more strictly when you assign one function to another. Example: a (x: string) => void handler cannot be used where a handler for string | number is expected.
  • useUnknownInCatchVariables makes the catch variable unknown instead of any. Example: in catch (e), you must check e instanceof Error before reading e.message.

Useful options outside strict:

  • noUncheckedIndexedAccess adds undefined to reads from arrays and index signatures. Example: arr[0] is T | undefined, because the array may be empty.
  • exactOptionalPropertyTypes separates a missing key from a key set to undefined. Example: with name?: string, writing { name: undefined } becomes an error.
  • noImplicitReturns gives an error when some code paths in a function return a value and others do not. Example: an if that returns, with no return after it.

For bundler setups, isolatedModules makes sure each file can be compiled on its own. Tools like Babel, esbuild and SWC compile one file at a time and need this.

  • Use strict: true as the baseline for every new project.
  • strictNullChecks means you must handle null and undefined.
  • noImplicitAny stops variables from silently becoming any.
  • noUncheckedIndexedAccess adds undefined to array and index reads, like arr[0].
  • isolatedModules makes each file compile on its own, for tools that compile one file at a time.