Загрузка…
Загрузка…
TypeScript · middle · сложность 5
// tsconfig.json — the baseline every new project should ship
{
"compilerOptions": {
"strict": true,
// Not in `strict`, but you almost always want them:
"noUncheckedIndexedAccess": true, // arr[i] is T | undefined — forces bounds handling
"noImplicitOverride": true, // must write `override` when overriding
"noFallthroughCasesInSwitch": true
}
}// strictNullChecks OFF → this compiles and crashes at runtime:
function greet(name: string) { return name.toUpperCase(); }
greet(null as any); // ❌ no error without the flag; 💥 TypeError at runtime
// strictNullChecks ON → the compiler forces the guard:
function greetSafe(name: string | null) {
if (name == null) return "hi"; // ✅ must narrow first
return name.toUpperCase();
}
// noUncheckedIndexedAccess ON → the off-by-one the type system now catches:
const xs = [1, 2, 3];
const first = xs[0]; // type is number | undefined, not number
first?.toFixed(); // ✅ handle undefinedstrict — umbrella; noUncheckedIndexedAccess часто важнее многих sub-flags; без strictNullChecks null silently проходит.