0
0
Typescriptprogramming~20 mins

Why type design patterns matter in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Design Mastery
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 a union type?
Consider this TypeScript code that uses a union type to accept different shapes of input. What will be logged to the console?
Typescript
type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; side: number };

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

console.log(area({ kind: 'circle', radius: 3 }));
ATypeError at runtime
B9
C28.274333882308138
Dundefined
Attempts:
2 left
💡 Hint
Think about the formula for the area of a circle and how the union type helps the function know which property to use.
🧠 Conceptual
intermediate
1:30remaining
Why use type guards in TypeScript?
Which of the following best explains why type guards are important in TypeScript type design patterns?
AThey allow the compiler to narrow down types so you can safely access properties specific to a type.
BThey automatically convert types at runtime to avoid errors.
CThey replace the need for interfaces in complex applications.
DThey prevent any type errors by disabling type checking.
Attempts:
2 left
💡 Hint
Think about how TypeScript knows which properties exist on a variable when it has multiple possible types.
🔧 Debug
advanced
2:00remaining
What error does this TypeScript code produce?
Look at this TypeScript code snippet using intersection types. What error will the compiler show?
Typescript
type A = { x: number };
type B = { y: string };
type C = A & B;

const obj: C = { x: 10, y: 20 };
ANo error, code compiles fine.
BType 'number' is not assignable to type 'string' for property 'y'.
CProperty 'x' is missing in type '{ x: 10, y: 20 }'.
DType 'string' is not assignable to type 'number' for property 'x'.
Attempts:
2 left
💡 Hint
Check the type of property 'y' in the object and in the type definition.
🚀 Application
advanced
2:30remaining
How to design a discriminated union for API responses?
You want to design a TypeScript type for API responses that can be either success or error. Which design pattern below correctly models this with discriminated unions?
Atype ApiResponse = { success: boolean; message: string };
Btype ApiResponse = { data: string } & { error: string };
Ctype ApiResponse = { status: string; data?: string; error?: string };
Dtype ApiResponse = { status: 'success'; data: string } | { status: 'error'; error: string };
Attempts:
2 left
💡 Hint
Discriminated unions use a common property with literal types to distinguish variants.
Predict Output
expert
3:00remaining
What is the output of this TypeScript code using conditional types?
Given this TypeScript code with conditional types, what will be the output when running the code?
Typescript
type IsString<T> = T extends string ? 'yes' : 'no';

function checkType<T>(value: T): IsString<T> {
  return (typeof value === 'string' ? 'yes' : 'no') as IsString<T>;
}

console.log(checkType(123));
console.log(checkType('hello'));
Ano\nyes
Byes\nno
CTypeError at runtime
Dundefined\nundefined
Attempts:
2 left
💡 Hint
Look at how the conditional type IsString works and how the function returns values based on typeof.