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)

1
Q

What is TypeScript’s strict mode?

A

Strict mode enables a set of type-checking options that enforce stricter rules, improving safety and reducing bugs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does ‘strict’ in tsconfig.json enable?

A

It enables all strict type-checking options like strictNullChecks, noImplicitAny, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the non-null assertion operator in TypeScript?

A

It’s an exclamation mark (!) used after a variable to assert that it’s not null or undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When should you use the non-null assertion operator?

A

Use it when you’re absolutely certain a value is not null or undefined, but TypeScript cannot infer it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are type guards in TypeScript?

A

They are techniques to narrow the type of a variable within a conditional block using typeof, instanceof, or custom checks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you create a custom type guard?

A

Use a function that returns ‘x is SomeType’ and performs necessary runtime checks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the ‘typeof’ type guard?

A

It’s used to narrow primitive types like number, string, boolean: e.g., typeof x === ‘string’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the ‘instanceof’ type guard?

A

It’s used to check if an object is an instance of a class or constructor function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is exhaustiveness checking?

A

It ensures all possible cases in a union type are handled, typically in a switch statement using the never type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you implement exhaustiveness checking in TypeScript?

A

Use a switch statement and a default case that assigns to a variable of type ‘never’ to catch unhandled cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between ‘unknown’ and ‘any’?

A

‘unknown’ is safer; you must narrow its type before usage, whereas ‘any’ disables type checking altogether.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s an advantage of ‘unknown’ over ‘any’?

A

‘unknown’ enforces explicit type checks, promoting safer code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a disadvantage of strict mode?

A

It may increase verbosity and development time, especially in large legacy codebases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a potential drawback of the non-null assertion operator?

A

It can lead to runtime errors if the value is actually null or undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a best practice for custom type guards?

A

Keep them simple, specific, and colocated with the types they guard.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a best practice when using ‘unknown’?

A

Always use type guards to narrow it before using the value.

17
Q

What are use cases for exhaustiveness checking?

A

State machines, Redux reducers, and discriminated union types where all variants must be handled.

18
Q

What is the impact of strict mode on system design?

A

It promotes cleaner, more predictable code and surfaces hidden assumptions early in development.

19
Q

How does ‘unknown’ affect architectural decisions?

A

It encourages defensive programming and explicit type validation at boundaries (e.g., API input).

20
Q

How does strict mode improve fault tolerance?

A

By catching potential null/undefined issues and type mismatches at compile-time before they reach production.

21
Q

How do these features help with debugging?

A

They reduce ambiguous runtime behavior by surfacing issues during development, making bugs easier to track.

22
Q

What is a real-world tradeoff with strict null checks?

A

You might have to rewrite or over-annotate legacy code to satisfy the compiler.

23
Q

What is a common interview question about TypeScript safety?

A

What is the difference between ‘any’ and ‘unknown’ and when would you use each?

24
Q

What is a gotcha with ‘instanceof’ type guard?

A

It doesn’t work reliably across execution contexts (e.g., iframes) and won’t work on interfaces.

25
What is a common mistake with the non-null assertion operator?
Using it too liberally and bypassing TypeScript’s safety checks, leading to runtime errors.
26
What is an example of exhaustiveness checking in action?
Using a switch on a discriminated union and ensuring the default case throws if a case is unhandled.
27
What architectural benefit do type guards provide?
They allow for more flexible but safe handling of polymorphic data, supporting open/closed design.
28
How do type guards impact performance?
They have minimal runtime cost and primarily affect compile-time safety and code clarity.
29
What is a best practice for handling 'any'?
Avoid it unless necessary, and prefer narrowing or using unknown with guards instead.
30
What is a performance implication of strict mode?
There is no runtime performance impact—it's purely a compile-time feature.
31
How can strict mode help with API integrations?
It forces you to handle missing or malformed data explicitly, reducing the chance of runtime failures.
32
What is a benefit of combining unknown and type guards?
You get both flexibility (handling any input) and safety (ensuring it's valid before using it).
33
What is a monitoring benefit of exhaustiveness checking?
If a new case is added to a union and not handled, the compiler can catch it before it leads to silent errors.
34
How can you enforce exhaustiveness without a switch?
Using if-else chains with assertNever functions to throw for unhandled cases.
35
What's a potential gotcha with strict mode in legacy code?
It may cause hundreds of errors when first enabled, requiring significant refactoring.
36
How can you enforce safer null handling without non-null assertions?
Use optional chaining, nullish coalescing, or proper type narrowing instead.
37
What is a real-world example where 'unknown' is preferred?
Handling user input from external sources like JSON.parse or API responses.
38
What happens if you forget to handle a case in exhaustiveness checking?
The compiler will throw an error if you use a 'never' type check to enforce it.
39
What should you watch for when using 'any' in third-party libraries?
They might hide bugs if the types are too permissive or outdated.