0
0
Typescriptprogramming~20 mins

Why patterns matter for safety in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pattern Safety Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code using pattern matching?
Consider this TypeScript code using a match-case style pattern with a discriminated union. What will be printed to the console?
Typescript
type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; size: number };

function area(shape: Shape): number {
  switch (shape.kind) {
    case 'circle':
      return Math.PI * shape.radius ** 2;
    case 'square':
      return shape.size ** 2;
  }
}

console.log(area({ kind: 'circle', radius: 3 }).toFixed(2));
ASyntax error: Unexpected token
B"28.27"
C"9.00"
DRuntime error: Property 'radius' does not exist on type 'Shape'
Attempts:
2 left
💡 Hint
Think about how the switch statement uses the 'kind' property to select the correct case.
Predict Output
intermediate
2:00remaining
What error does this unsafe pattern cause?
What error will this TypeScript code produce when run, considering the unsafe access pattern?
Typescript
type User = { name: string; age?: number };

function getAge(user: User) {
  return user.age!.toFixed(0);
}

console.log(getAge({ name: 'Alice' }));
ATypeError: Cannot read property 'toFixed' of undefined
Bundefined
CSyntaxError: Unexpected token '.'
DNo error, prints '0'
Attempts:
2 left
💡 Hint
Check what happens if 'age' is missing and you try to call a method on it.
🧠 Conceptual
advanced
2:00remaining
Why do patterns help prevent runtime errors in TypeScript?
Which statement best explains why using patterns like discriminated unions and exhaustive checks improves safety in TypeScript?
AThey allow the compiler to verify all possible cases are handled, preventing unexpected undefined values.
BThey remove the need for type annotations entirely.
CThey make the code run faster by optimizing loops automatically.
DThey allow any type to be assigned without errors.
Attempts:
2 left
💡 Hint
Think about how the compiler helps catch missing cases.
Predict Output
advanced
2:00remaining
What is the output of this TypeScript code using pattern guards?
What will this code print to the console?
Typescript
type Response = { status: 'success'; data: string } | { status: 'error'; message: string };

function handleResponse(resp: Response) {
  if (resp.status === 'success') {
    return resp.data.toUpperCase();
  } else {
    return resp.message.toLowerCase();
  }
}

console.log(handleResponse({ status: 'error', message: 'NOT FOUND' }));
ARuntime error: Cannot read property 'toLowerCase' of undefined
B"NOT FOUND"
C"not found"
DSyntax error: Missing return statement
Attempts:
2 left
💡 Hint
Check which branch runs for status 'error' and what method is called.
🧠 Conceptual
expert
3:00remaining
How do patterns in TypeScript improve code safety in large applications?
Select the best explanation for how using patterns like discriminated unions, exhaustive checks, and type guards improves safety in large TypeScript codebases.
AThey replace the need for unit testing entirely.
BThey automatically generate UI components based on types.
CThey allow ignoring type errors during compilation for faster builds.
DThey enforce strict handling of all possible data shapes, reducing bugs and improving maintainability.
Attempts:
2 left
💡 Hint
Think about how patterns help manage complexity and prevent errors.