Browse topics

TypeScript strictness options

Version
es2025
Since
2.3
Review
Draft

Summary#

strict enables a group of TypeScript checks, including noImplicitAny and strictNullChecks. Enable noUncheckedIndexedAccess and exactOptionalPropertyTypes separately for tighter checks on indexed reads and optional-property writes.

Syntax#

json
{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true
  }
}

Example#

Run this shell example with Node.js and your project's TypeScript compiler (tsc) on your path.

sh
(
  set -eu
  work_dir=$(mktemp -d "${TMPDIR:-/tmp}/dlr-js-ts-core-XXXXXXXX")
  trap 'rm -rf -- "$work_dir"' 0
  trap 'exit 1' HUP INT TERM
  cd "$work_dir"
  export TMPDIR="$PWD"
  unset NODE_COMPILE_CACHE

  cat > limit.ts <<'TS'
function chooseLimit(overrides: { limit?: number }, samples: number[]): number {
  return overrides.limit ?? samples[0] ?? 10;
}

console.log(chooseLimit({ limit: 0 }, [3]));
console.log(chooseLimit({}, []));
TS

  tsc --strict --noUncheckedIndexedAccess --exactOptionalPropertyTypes \
    --target es2015 --module commonjs --noEmitOnError --outDir out limit.ts
  node out/limit.js
)

Expected output#

Text
0
10

Common mistakes#

With checked indexed access, an array lookup can yield undefined. Check the result or supply a fallback. With exact optional properties, omit limit to use its fallback. An explicit limit: undefined requires undefined in the declared property type. This option requires strictNullChecks.

Version applicability#

TypeScript 2.3 added strict. TypeScript 6.0 enables it by default. TypeScript 4.1 added noUncheckedIndexedAccess, and 4.4 added exactOptionalPropertyTypes. The full example needs TypeScript 4.4 or later and emits ES2015 JavaScript.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0