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.
✗ 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?
A??
B!!
C?.
D||
✗ Incorrect
The optional chaining operator ?. safely accesses properties without errors if the object is null or undefined.
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>.
✗ Incorrect
The ?? operator returns the right side only if the left side is null or undefined.
Which TypeScript setting enables strict null checks?
AstrictNullChecks
BnoImplicitAny
CnoUnusedLocals
DallowJs
✗ 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?
AUse it directly without checks.
BCheck if it is not <code>null</code>.
CAssign it to <code>undefined</code>.
DConvert it to a number.
✗ 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.
Practice
(1/5)
1.
What does enabling strictNullChecks in TypeScript do?
easy
A. It allows assigning null to any type without errors.
B. It automatically converts all null values to empty strings.
C. It disables type checking for null and undefined.
D. It forces you to explicitly handle null and undefined values.
Solution
Step 1: Understand strictNullChecks purpose
Enabling strictNullChecks makes TypeScript require explicit handling of null and undefined values to avoid runtime errors.
Step 2: Analyze options
Only It forces you to explicitly handle null and undefined values. correctly describes this behavior. Options B, C, and D are incorrect because they describe behaviors that strictNullChecks does not do.
Final Answer:
It forces you to explicitly handle null and undefined values. -> Option D
Quick Check:
strictNullChecks = explicit null handling [OK]
Hint: Remember: strictNullChecks means no silent nulls [OK]
Common Mistakes:
Thinking nulls are automatically converted
Assuming null checks are disabled
Believing null can be assigned anywhere
2.
Which of the following is the correct way to check for null or undefined before accessing a variable value in TypeScript with strictNullChecks enabled?
let value: string | null | undefined;
easy
A. if (value !== null && value !== undefined) { console.log(value.length); }
B. if (value) { console.log(value.length); }
C. console.log(value.length);
D. if (value == null) { console.log(value.length); }
Solution
Step 1: Understand null and undefined checks
With strictNullChecks, you must check both null and undefined explicitly before accessing properties.
Step 2: Evaluate each option
if (value !== null && value !== undefined) { console.log(value.length); } correctly checks both null and undefined. if (value) { console.log(value.length); } may fail if value is an empty string. console.log(value.length); causes an error. if (value == null) { console.log(value.length); } checks for null but then accesses length inside the if, which is incorrect logic.
Final Answer:
if (value !== null && value !== undefined) { console.log(value.length); } -> Option A
Quick Check:
Check both null and undefined explicitly [OK]
Hint: Use !== null && !== undefined to be safe [OK]
Common Mistakes:
Using only if(value) which fails on empty strings
Accessing properties without checks
Using == null but then accessing inside that block
3.
What will be the output of the following TypeScript code with strictNullChecks enabled?
A. Use non-null assertion: return name!.toUpperCase();
B. Change parameter type to string only and remove null and undefined.
C. Add a check: if (name == null) return "Unknown"; before calling toUpperCase().
D. Remove strictNullChecks from tsconfig.
Solution
Step 1: Identify unsafe code
Calling toUpperCase() on name without checking for null or undefined causes errors with strictNullChecks.
Step 2: Evaluate options for safety
Add a check: if (name == null) return "Unknown"; before calling toUpperCase(). adds a check for both null and undefined (using == null) and returns a default string, safely avoiding errors. Change parameter type to string only and remove null and undefined. removes null safety but is less flexible. Use non-null assertion: return name!.toUpperCase(); forces non-null assertion which can cause runtime errors. Remove strictNullChecks from tsconfig. disables safety feature, not recommended.
Final Answer:
Add a check: if (name == null) return "Unknown"; before calling toUpperCase(). -> Option C
Quick Check:
Check null/undefined before method calls [OK]
Hint: Use if(name == null) to check null and undefined together [OK]
Common Mistakes:
Using non-null assertion unsafely
Removing null from type instead of checking
Disabling strictNullChecks instead of fixing code
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.