Error Handling, Safety & Strict Mode Flashcards
strict mode Non-null Assertion Operator Type Guards (typeof, instanceof, custom guards) Exhaustiveness Checking unknown vs any (39 cards)
What is TypeScript’s strict mode?
Strict mode enables a set of type-checking options that enforce stricter rules, improving safety and reducing bugs.
What does ‘strict’ in tsconfig.json enable?
It enables all strict type-checking options like strictNullChecks, noImplicitAny, etc.
What is the non-null assertion operator in TypeScript?
It’s an exclamation mark (!) used after a variable to assert that it’s not null or undefined.
When should you use the non-null assertion operator?
Use it when you’re absolutely certain a value is not null or undefined, but TypeScript cannot infer it.
What are type guards in TypeScript?
They are techniques to narrow the type of a variable within a conditional block using typeof, instanceof, or custom checks.
How do you create a custom type guard?
Use a function that returns ‘x is SomeType’ and performs necessary runtime checks.
What is the ‘typeof’ type guard?
It’s used to narrow primitive types like number, string, boolean: e.g., typeof x === ‘string’.
What is the ‘instanceof’ type guard?
It’s used to check if an object is an instance of a class or constructor function.
What is exhaustiveness checking?
It ensures all possible cases in a union type are handled, typically in a switch statement using the never type.
How do you implement exhaustiveness checking in TypeScript?
Use a switch statement and a default case that assigns to a variable of type ‘never’ to catch unhandled cases.
What is the difference between ‘unknown’ and ‘any’?
‘unknown’ is safer; you must narrow its type before usage, whereas ‘any’ disables type checking altogether.
What’s an advantage of ‘unknown’ over ‘any’?
‘unknown’ enforces explicit type checks, promoting safer code.
What is a disadvantage of strict mode?
It may increase verbosity and development time, especially in large legacy codebases.
What is a potential drawback of the non-null assertion operator?
It can lead to runtime errors if the value is actually null or undefined.
What is a best practice for custom type guards?
Keep them simple, specific, and colocated with the types they guard.
What is a best practice when using ‘unknown’?
Always use type guards to narrow it before using the value.
What are use cases for exhaustiveness checking?
State machines, Redux reducers, and discriminated union types where all variants must be handled.
What is the impact of strict mode on system design?
It promotes cleaner, more predictable code and surfaces hidden assumptions early in development.
How does ‘unknown’ affect architectural decisions?
It encourages defensive programming and explicit type validation at boundaries (e.g., API input).
How does strict mode improve fault tolerance?
By catching potential null/undefined issues and type mismatches at compile-time before they reach production.
How do these features help with debugging?
They reduce ambiguous runtime behavior by surfacing issues during development, making bugs easier to track.
What is a real-world tradeoff with strict null checks?
You might have to rewrite or over-annotate legacy code to satisfy the compiler.
What is a common interview question about TypeScript safety?
What is the difference between ‘any’ and ‘unknown’ and when would you use each?
What is a gotcha with ‘instanceof’ type guard?
It doesn’t work reliably across execution contexts (e.g., iframes) and won’t work on interfaces.