What strict turns on in TypeScript
The individual flags behind strict, shown by what each one catches.
// strictNullChecks: null is not assignable to string
function upper(value: string | null): string {
return value === null ? "" : value.toUpperCase();
}
// noUncheckedIndexedAccess: rows[0] is string | undefined
function firstLine(rows: string[]): string {
const first = rows[0];
return first ?? "(empty)";
}
// strictFunctionTypes: parameter positions are checked contravariantly
type Handler = (value: string) => void;
const wide: (value: unknown) => void = (value) => console.log(value);
const narrowed: Handler = wide;
// noImplicitAny: an untyped parameter is an error, so annotate it
function total(values: readonly number[]): number {
return values.reduce((sum, value) => sum + value, 0);
}
console.log(upper(null), firstLine([]), total([1, 2]));
narrowed("ok");
How it works
strictNullChecksseparates null from every other type.noUncheckedIndexedAccessmakes an index read possibly undefined.exactOptionalPropertyTypesstops undefined standing in for absent.
Keywords and builtins used here
Handlerconstfunctionnumberreadonlyreturnstringtypeunknown
The run, in numbers
- Lines
- 23
- Characters to type
- 773
- Tokens
- 158
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 95 seconds.
Step 2 of 2 in satisfies & strictness, step 22 of 23 in Types & annotations.