0
0
Typescriptprogramming~20 mins

TypeScript Strict Mode and Why It Matters - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TypeScript Strict Mode Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
ATypeError at runtime: Cannot read property 'toUpperCase' of undefined
BHello, UNDEFINED
CCompilation error: Argument of type 'undefined' is not assignable to parameter of type 'string'.
DHello, undefined
Attempts:
2 left
💡 Hint
'any' bypasses type checks, even in strict mode.
🧠 Conceptual
intermediate
1:30remaining
Why does strictNullChecks matter in TypeScript strict mode?
Which statement best explains the role of the strictNullChecks option in TypeScript strict mode?
AIt automatically converts null and undefined to empty strings.
BIt allows null and undefined to be assigned to any type without errors.
CIt disables all type checking related to null and undefined.
DIt requires explicit handling of null and undefined values, preventing accidental runtime errors.
Attempts:
2 left
💡 Hint
Think about how strictNullChecks changes how null and undefined are treated in types.
🔧 Debug
advanced
2: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);
ARuntime error: Cannot read property 'length' of undefined.
BCompilation error: Object is possibly 'undefined'.
CNo error, outputs undefined.
DCompilation error: Property 'length' does not exist on type 'string | undefined'.
Attempts:
2 left
💡 Hint
Check how optional properties are treated in strict mode.
📝 Syntax
advanced
1: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
Aconsole.log(user.email!.length);
Bconsole.log(user.email.length);
Cconsole.log(user.email?.length);
Dconsole.log(user.email.length!);
Attempts:
2 left
💡 Hint
Non-null assertion operator tells TypeScript the value is not null or undefined.
🚀 Application
expert
2: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;
}
A1 error
B3 errors
C2 errors
DNo errors
Attempts:
2 left
💡 Hint
Check each line for type mismatches or unsafe operations under strict mode.