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?✗ Incorrect
With
strictNullChecks enabled, null is not assignable to string unless explicitly allowed.Which operator safely accesses a property of an object that might be
null or undefined?✗ Incorrect
The optional chaining operator
?. safely accesses properties without errors if the object is null or undefined.What does the nullish coalescing operator
?? do?✗ Incorrect
The
?? operator returns the right side only if the left side is null or undefined.Which TypeScript setting enables strict null checks?
✗ Incorrect
The
strictNullChecks compiler option enables strict checking of null and undefined.If a variable is declared as
string | null, what must you do before using it as a string?✗ Incorrect
You must check the variable is not
null before using it as a string to avoid errors.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.