Challenge - 5 Problems
TypeScript Strict Mode Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output when strict mode is enabled?
Consider this TypeScript code with strict mode enabled. What will be the output when compiled and run?
Typescript
function greet(name: string) { return `Hello, ${name.toUpperCase()}`; } console.log(greet(undefined as any));
Attempts:
2 left
💡 Hint
'any' bypasses type checks, even in strict mode.
✗ Incorrect
The 'as any' cast bypasses TypeScript's type checking, so the code compiles. At runtime, 'name' is undefined, causing a TypeError on toUpperCase().
🧠 Conceptual
intermediate1:30remaining
Why does strictNullChecks matter in TypeScript strict mode?
Which statement best explains the role of the strictNullChecks option in TypeScript strict mode?
Attempts:
2 left
💡 Hint
Think about how strictNullChecks changes how null and undefined are treated in types.
✗ Incorrect
strictNullChecks forces you to explicitly check or handle null and undefined, reducing bugs caused by unexpected null values.
🔧 Debug
advanced2:00remaining
Identify the error caused by strict mode
What error will this TypeScript code produce when strict mode is enabled?
Typescript
let data: { id: number; name?: string } = { id: 1 }; console.log(data.name.length);
Attempts:
2 left
💡 Hint
Check how optional properties are treated in strict mode.
✗ Incorrect
Because 'name' is optional, it might be undefined. Strict mode requires you to check before accessing 'length'.
📝 Syntax
advanced1:30remaining
Which code snippet correctly uses strict mode with non-null assertion?
Given strict mode is enabled, which option correctly accesses a possibly undefined variable without error?
Typescript
let user: { email?: string } = {}; // Access user.email length safely
Attempts:
2 left
💡 Hint
Non-null assertion operator tells TypeScript the value is not null or undefined.
✗ Incorrect
Option A uses the non-null assertion operator (!) correctly to tell TypeScript that user.email is not undefined here.
🚀 Application
expert2:30remaining
How many errors will strict mode report in this code?
Count the number of TypeScript compilation errors when strict mode is enabled for this code:
Typescript
function process(input: string | null) { console.log(input.toUpperCase()); let count: number = null; let value: any = 5; let result: number = value; }
Attempts:
2 left
💡 Hint
Check each line for type mismatches or unsafe operations under strict mode.
✗ Incorrect
1) input.toUpperCase() is unsafe because input can be null.
2) Assigning null to number is invalid.
No error on assigning any to number, as any is assignable to any type.
Total: 2 errors.