typestar

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

  1. strictNullChecks separates null from every other type.
  2. noUncheckedIndexedAccess makes an index read possibly undefined.
  3. exactOptionalPropertyTypes stops undefined standing in for absent.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 2 in satisfies & strictness, step 22 of 23 in Types & annotations.

← Previous Next →