0
0
Typescriptprogramming~20 mins

Why advanced types are needed in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Advanced TypeScript 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 union types?
Consider this TypeScript code snippet that uses union types. What will be the output when calling formatValue with 42 and "hello"?
Typescript
function formatValue(value: number | string): string {
  if (typeof value === "number") {
    return `Number: ${value.toFixed(2)}`;
  } else {
    return `String: ${value.toUpperCase()}`;
  }
}

console.log(formatValue(42));
console.log(formatValue("hello"));
A
Number: 42.00
String: HELLO
BError: toFixed is not a function
C
42.00
HELLO
D
Number: 42
String: hello
Attempts:
2 left
💡 Hint
Think about how union types allow different behaviors based on type checks.
🧠 Conceptual
intermediate
1:30remaining
Why use advanced types like intersection types in TypeScript?
Which of the following best explains why intersection types are useful in TypeScript?
AThey let you choose one type from many possible types at runtime.
BThey automatically convert types to strings for easier display.
CThey prevent any type from being assigned to a variable.
DThey allow combining multiple types into one, ensuring the value has all properties from each type.
Attempts:
2 left
💡 Hint
Think about how you can require an object to have properties from multiple types.
🔧 Debug
advanced
2:00remaining
What error does this TypeScript code raise?
Look at this code using conditional types. What error will TypeScript report?
Typescript
type IsString<T> = T extends string ? true : false;

let test1: IsString<number> = true;
let test2: IsString<string> = false;
AType 'true' is not assignable to type 'false'.
B
Type 'true' is not assignable to type 'false'.
Type 'false' is not assignable to type 'true'.
CNo error, code compiles fine.
DType 'false' is not assignable to type 'true'.
Attempts:
2 left
💡 Hint
Check the expected types for test1 and test2 based on the conditional type.
📝 Syntax
advanced
1:30remaining
Which option correctly defines a mapped type in TypeScript?
You want to create a mapped type that makes all properties of a type optional. Which option is correct?
Atype Partial<T> = { (P in keyof T)?: T[P] };
Btype Partial<T> = { [P of keyof T]?: T[P] };
Ctype Partial<T> = { [P in keyof T]?: T[P] };
Dtype Partial<T> = { [P in T]?: T[P] };
Attempts:
2 left
💡 Hint
Remember the syntax for mapped types uses 'in' and square brackets.
🚀 Application
expert
2:30remaining
How many properties does the resulting type have?
Given these types and their intersection, how many properties does the type Combined have?
Typescript
type A = { x: number; y: string };
type B = { y: string; z: boolean };
type Combined = A & B;
A3 properties: x, y, and z
B2 properties: x and z only
C4 properties: x, y, z, and an extra y
D1 property: y only
Attempts:
2 left
💡 Hint
Intersection types combine all properties, but overlapping keys must be compatible.