Careeroza
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'.| Flag | Catches |
|---|---|
noUncheckedIndexedAccess | reading array/record items that may not exist |
exactOptionalPropertyTypes | explicitly assigning undefined to optional props |
noImplicitReturns | functions where some paths do not return |
noFallthroughCasesInSwitch | case without break/return |
noUnusedLocals, noUnusedParameters | dead variables and parameters |
noImplicitOverride | overriding a method without the override keyword |
noPropertyAccessFromIndexSignature | forces obj["key"] for dynamic keys |