Strict mode

strict:true

One flag turns on the whole strict family at once. New projects should start with it enabled, because every strict error is a runtime crash avoided.

The command below checks the current project with full strictness without writing any files.

tsc --strict --noEmit

On a clean project the command prints nothing; every strict error it finds appears with its file, line, and column, and no files are written.

strictNullChecks

Without null checks, null and undefined hide inside every type. With the flag on, a value that may be missing must be declared and handled before use.

Narrow first, then act. An early return for the empty case keeps the happy path clean and fully typed.

function greet(name: string | null): string { if (name === null) { return "Guest"; } return name.toUpperCase(); } console.log(greet(null)); console.log(greet("ada"));

Guest
ADA

noUncheckedIndexedAccess

Array access looks safe but can read past the end. This flag adds undefined to every index read, so the checker forces a guard for missing elements.

The extra check feels noisy for a week and then becomes a habit. Out of range reads turn into compile errors instead of silent undefined values.

const scores: number[] = [10, 20]; const first = scores[0]; if (first !== undefined) { console.log(first * 2); }

20

Adopt file by file

Turning strict on for a large old codebase can flood the terminal with errors. Adopt it in slices instead of one painful rewrite.

Enable strict in the config, fix one folder, and keep new files strict from day one. The error count drops steadily while the code keeps shipping.

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

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does "strict": true turn on?

    Answer

    The whole family of strict checks at once

    The single flag enables the strict checks together, so a new project starts with them.

  2. What does strictNullChecks require?

    Answer

    That null and undefined are handled before a value is used

    The flag removes null and undefined from every type unless they are declared and checked.

  3. What does noUncheckedIndexedAccess add to every indexed read?

    Answer

    undefined in the type, so a guard is needed

    An index can be past the end, so the read is typed as the value or undefined.

  4. How does a large old codebase adopt strict mode best?

    Answer

    Turn it on folder by folder and keep new files strict

    Fixing one slice at a time keeps the build shipping while the error count falls.

  5. What does this program print? function greet(name: string | null): string { if (name === null) { return "Guest"; } return name.toUpperCase(); } console.log(greet(null)); console.log(greet("ada"));

    Answer

    Guest / ADA

    The null case returns Guest, and the string case is uppercased to ADA.

  6. What does this program print? function greet(name: string | null): string { if (name === null) { return "Guest"; } return name.toUpperCase(); } console.log(greet("Ada"));

    Answer

    ADA

    The string case reaches toUpperCase, which returns ADA.

  7. What does this program print? const scores: number[] = [10, 20]; const first = scores[0]; if (first !== undefined) { console.log(first * 2); }

    Answer

    20

    The first element is 10, and doubling it gives 20.

  8. What does this program print? const name: string | null = null; console.log(name ?? "anon");

    Answer

    anon

    The nullish operator returns the right side when the left is null.

  9. What does the compiler say about this program? const name: string | null = null; console.log(name.toUpperCase());

    Answer

    error TS18047: 'name' is possibly 'null'.

    strictNullChecks keeps null in the type, so the value must be checked before the call.

  10. What does the compiler say about this program? const s: string = null; console.log(s);

    Answer

    error TS2322: Type 'null' is not assignable to type 'string'.

    With strictNullChecks a string variable cannot hold null.

Article author: Arthur Isaev