0
0
Typescriptprogramming~20 mins

Strict null checks and safety in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Null Safety Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output with strict null checks enabled
What is the output of this TypeScript code when strict null checks are enabled?
Typescript
function greet(name: string | null) {
  if (name === null) {
    return "Hello, guest!";
  }
  return `Hello, ${name.toUpperCase()}!`;
}

console.log(greet(null));
console.log(greet("alice"));
ATypeError at runtime
BHello, null!\nHello, ALICE!
CHello, guest!\nHello, alice!
DHello, guest!\nHello, ALICE!
Attempts:
2 left
💡 Hint
Check how the function handles the null case before calling toUpperCase.
Predict Output
intermediate
1:30remaining
Value of variable after null assertion
What is the value of variable 'length' after running this code with strict null checks enabled?
Typescript
let maybeString: string | null = "hello";
let length = maybeString!.length;
A5
Bundefined
CTypeError at runtime
DCompilation error
Attempts:
2 left
💡 Hint
The exclamation mark tells TypeScript the value is not null here.
Predict Output
advanced
2:00remaining
Output of function with optional chaining and strict null checks
What is the output of this TypeScript code with strict null checks enabled?
Typescript
type User = { name: string; address?: { city: string } };

function getCity(user: User): string {
  return user.address?.city ?? "Unknown city";
}

console.log(getCity({ name: "Bob", address: { city: "Paris" } }));
console.log(getCity({ name: "Alice" }));
ATypeError at runtime
BParis\nundefined
CParis\nUnknown city
DCompilation error
Attempts:
2 left
💡 Hint
Optional chaining safely accesses nested properties that might be missing.
Predict Output
advanced
1:30remaining
Result of filtering null values with strict null checks
What is the output of this TypeScript code with strict null checks enabled?
Typescript
const values: (string | null)[] = ["a", null, "b", null, "c"];
const filtered = values.filter(v => v !== null);
console.log(filtered);
A[null, null]
B["a", "b", "c"]
C["a", null, "b", null, "c"]
DCompilation error due to type mismatch
Attempts:
2 left
💡 Hint
The filter removes all null values from the array.
🧠 Conceptual
expert
2:30remaining
Why does this code cause a compilation error with strict null checks?
Consider this TypeScript code with strict null checks enabled: let input: string | null = null; let length: number = input.length; Why does this cause a compilation error?
ABecause input might be null, and accessing length on null is unsafe
BBecause length is not a valid property of string
CBecause null is not allowed as a value for string type
DBecause TypeScript does not allow variables to be assigned before declaration
Attempts:
2 left
💡 Hint
Think about what happens if input is null when accessing a property.