0
0
Typescriptprogramming~20 mins

Why advanced utility types matter in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Advanced Utility Types 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 utility types?

Consider this TypeScript code using Partial and Required utility types. What will be the output when running the code?

Typescript
type Person = {
  name: string;
  age?: number;
};

const partialPerson: Partial<Person> = { name: "Alice" };
const requiredPerson: Required<Person> = { name: "Bob", age: 30 };

console.log(partialPerson.age);
console.log(requiredPerson.age);
A30\n30
Bundefined\n30
Cundefined\nundefined
DError: Property 'age' is missing in type '{ name: string; }' but required in type 'Required<Person>'
Attempts:
2 left
💡 Hint

Think about what Partial and Required do to the properties of a type.

🧠 Conceptual
intermediate
1:30remaining
Why use the Pick utility type?

Which of the following best explains why the Pick utility type is useful in TypeScript?

AIt converts all properties of a type to readonly.
BIt removes all optional properties from a type.
CIt creates a new type by selecting specific properties from an existing type.
DIt merges two types into one.
Attempts:
2 left
💡 Hint

Think about how you can create smaller types from bigger ones.

🔧 Debug
advanced
2:00remaining
What error does this TypeScript code produce?

Look at this code using Record utility type. What error will TypeScript show?

Typescript
type Roles = 'admin' | 'user';

const rolePermissions: Record<Roles, string[]> = {
  admin: ['read', 'write'],
  user: ['read'],
  guest: ['read']
};
AError: Object literal may only specify known properties, and 'guest' does not exist in type 'Record<Roles, string[]>'
BNo error, code compiles fine.
CError: Missing property 'user' in object.
DError: Type 'string[]' is not assignable to type 'string'.
Attempts:
2 left
💡 Hint

Check if all keys in the object match the keys in the Record type.

📝 Syntax
advanced
1:30remaining
Which option correctly uses the Exclude utility type?

Given type T = 'a' | 'b' | 'c', which option correctly excludes 'b' from T?

Atype U = Exclude<'b', T>; // 'a' | 'c'
Btype U = Exclude<T, 'd'>; // 'a' | 'b' | 'c'
Ctype U = Exclude<T, 'a' | 'c'>; // 'b'
Dtype U = Exclude<T, 'b'>; // 'a' | 'c'
Attempts:
2 left
💡 Hint

Remember the order of parameters in Exclude: first is the union, second is what to remove.

🚀 Application
expert
2:30remaining
How many properties does this mapped type have?

Given this code, how many properties does the type Flags have?

Typescript
type Keys = 'featureA' | 'featureB' | 'featureC';

type Flags = {
  [K in Keys as K extends 'featureB' ? never : K]: boolean;
};
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Look at the conditional key remapping in the mapped type.