Strict mode
Strict mode is one line in tsconfig.json, "strict": true, that turns on a family of 9 stricter checks. Follow a six-line program that passes tsc with strict off and then crashes with a TypeError, and watch two of those checks, noImplicitAny and strictNullChecks, point at the exact lines before it runs. Handling the missing case makes it print NOBODY.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01What does "strict": true in tsconfig.json do?
It is one switch for a family of 9 checks in TypeScript 5.9, including noImplicitAny and strictNullChecks. Your code and the language stay the same; tsc just checks harder.
02With strict off, why did tsc report zero errors for code that crashes?
Without noImplicitAny an untyped parameter quietly becomes any, which turns checking off for it. Without strictNullChecks, undefined is allowed where a string is expected, so find's missing result goes unnoticed.
03Under strict mode, what type does names.find(n => n === "Bo") have, for names: string[]?
find returns the first match, or undefined when nothing matches. strictNullChecks makes that visible in the type: string | undefined.
04Which fix makes shout(who) pass strictNullChecks and print NOBODY here?
?? uses the right side when the left is null or undefined, so the argument is always a string. "as any" and turning strict off only hide the problem, and the program would still crash.
05You turn strict on in a large older project and get hundreds of errors. What is a sensible plan?
A check set on its own overrides strict, so "strict": true with "strictNullChecks": false keeps every other check. Fix the errors file by file, then remove the override.
Glossary
- Strict mode
- The "strict": true setting in tsconfig.json. It turns on a family of stricter type checks at once.
- tsconfig.json
- The settings file for a TypeScript project. tsc reads it to know which files to check and how.
- any
- A type that switches checking off: any value fits, and any use of it is allowed.
- noImplicitAny
- A strict check: it is an error when TypeScript would silently give something the type any.
- strictNullChecks
- A strict check: null and undefined get their own types, so code must handle them before using a value.
- Union type
- A type that is one of several, written with |. string | undefined is a string or undefined.
- Nullish coalescing (??)
- a ?? b gives b when a is null or undefined, and a otherwise.
- TypeError
- The JavaScript error raised when a value is used in a way its type does not allow, such as calling a method on undefined.
Notes and sources
- The list of nine is TypeScript 5.9's. New TypeScript versions sometimes add a check to the strict family, so upgrading can show new errors in strict projects.
- Strict mode only changes the checking, with one small exception: alwaysStrict makes tsc put "use strict"; at the top of each JavaScript file it writes.
- The error texts are exactly what tsc 5.9 prints for these files. By default tsc still writes app.js even when it reports errors (noEmitOnError is off).
- The code is real and runnable. With strict off it passes the check and then crashes with TypeError: Cannot read properties of undefined (reading 'toUpperCase'); the fixed program prints NOBODY.
- TSConfig Reference: strict
- TSConfig Reference: noImplicitAny
- TSConfig Reference: strictNullChecks
- TSConfig Reference: alwaysStrict
- TypeScript Handbook: The Basics, Strictness
- TypeScript Handbook: Everyday Types (union types, null and undefined)
- TypeScript Handbook: Narrowing
- MDN: Array.prototype.find()
- MDN: Nullish coalescing operator (??)
- MDN: TypeError
Quiz Unlocks after the last step.