Extra Safety Flags

noUncheckedIndexedAccess

Not part of strict but highly recommended. Index access on arrays and records adds | undefined, because the element may not exist.

Example

With the flag on, prices[5] is number | undefined and stock["SKU-9"] too, so using them directly is an error (TS2532: Object is possibly undefined).

const prices = [499, 89];
const stock: Record<string, number> = { "SKU-1": 4 };

console.log(prices[5].toFixed(2));
console.log(stock["SKU-9"] + 1);

Compiler output (tsc)

Line 4: error TS2532: Object is possibly 'undefined'.
Line 5: error TS2532: Object is possibly 'undefined'.

exactOptionalPropertyTypes and Others

Example

With exactOptionalPropertyTypes, theme?: "dark" means "may be missing" but NOT "may be set to undefined". noImplicitReturns catches code paths that forget to return.

type Prefs = { theme?: "light" | "dark" };
const p: Prefs = { theme: undefined };

function discount(tier: "gold" | "silver" | "none"): number {
  if (tier === "gold") return 20;
  if (tier === "silver") return 10;
}

Compiler output (tsc)

Line 2: error TS2375: Type '{ theme: undefined; }' is not assignable to type 'Prefs' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
  Types of property 'theme' are incompatible.
    Type 'undefined' is not assignable to type '"light" | "dark"'.
Line 4: error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
FlagCatches
noUncheckedIndexedAccessreading array/record items that may not exist
exactOptionalPropertyTypesexplicitly assigning undefined to optional props
noImplicitReturnsfunctions where some paths do not return
noFallthroughCasesInSwitchcase without break/return
noUnusedLocals, noUnusedParametersdead variables and parameters
noImplicitOverrideoverriding a method without the override keyword
noPropertyAccessFromIndexSignatureforces obj["key"] for dynamic keys