0
0
Typescriptprogramming~5 mins

Strict null checks and safety in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does enabling strictNullChecks in TypeScript do?
It makes TypeScript treat null and undefined as distinct types that must be explicitly handled, preventing accidental errors from uninitialized or missing values.
Click to reveal answer
beginner
How do you safely access a property of an object that might be null or undefined in TypeScript with strict null checks?
You can use the optional chaining operator ?. to safely access properties without causing runtime errors if the object is null or undefined.
Click to reveal answer
beginner
What is a common way to provide a fallback value when a variable might be null or undefined?
Use the nullish coalescing operator ?? to provide a default value only when the variable is null or undefined.
Click to reveal answer
intermediate
Why is it safer to enable strictNullChecks compared to having it disabled?
Because it forces you to handle null and undefined explicitly, reducing bugs caused by unexpected missing values and improving code reliability.
Click to reveal answer
beginner
What TypeScript type can represent a variable that might be a string or might be null?
You can use a union type like string | null to explicitly say the variable can hold either a string or null.
Click to reveal answer
What happens if you try to assign null to a variable of type string with strictNullChecks enabled?
ATypeScript shows an error.
BIt works without any error.
CThe variable becomes <code>undefined</code>.
DThe code compiles but throws a runtime error.
Which operator safely accesses a property of an object that might be null or undefined?
A??
B!!
C?.
D||
What does the nullish coalescing operator ?? do?
AThrows an error if the value is <code>null</code>.
BReturns the right value if the left is falsy.
CChecks if a value is <code>null</code>.
DReturns the right value if the left is <code>null</code> or <code>undefined</code>.
Which TypeScript setting enables strict null checks?
AstrictNullChecks
BnoImplicitAny
CnoUnusedLocals
DallowJs
If a variable is declared as string | null, what must you do before using it as a string?
AUse it directly without checks.
BCheck if it is not <code>null</code>.
CAssign it to <code>undefined</code>.
DConvert it to a number.
Explain how strictNullChecks improves safety in TypeScript code.
Think about how TypeScript treats missing values with this setting.
You got /4 concepts.
    Describe how to safely access a property of an object that might be null or undefined.
    Consider the operator that stops property access if the object is missing.
    You got /3 concepts.