Challenge - 5 Problems
Master of Combining Utility Types
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of combined Partial and Readonly types
What is the output of the following TypeScript code when compiled and run with Node.js (ignoring type errors)?
Typescript
type User = {
id: number;
name: string;
email: string;
};
const user: Readonly<Partial<User>> = { id: 1 };
console.log(user);Attempts:
2 left
💡 Hint
Partial makes all properties optional, Readonly makes them immutable.
✗ Incorrect
Partial allows any subset of User properties. Readonly makes those properties immutable but does not add missing properties. So the object has only 'id'.
❓ Predict Output
intermediate2:00remaining
Resulting type keys after combining Pick and Required
Given the following TypeScript code, what keys does the type 'Result' have?
Typescript
type Product = {
id?: number;
name?: string;
price?: number;
};
type Result = Required<Pick<Product, 'id' | 'price'>>;Attempts:
2 left
💡 Hint
Pick selects keys, Required makes them non-optional.
✗ Incorrect
Pick selects only 'id' and 'price' keys, still optional. Required makes those keys required (non-optional).
🔧 Debug
advanced2:00remaining
Why does this combined utility type cause an error?
Consider this TypeScript code snippet. Why does it cause a compilation error?
Typescript
type Config = {
url: string;
timeout?: number;
};
type NewConfig = Readonly<Required<Config>>;
const config: NewConfig = {
url: "https://example.com"
};Attempts:
2 left
💡 Hint
Required makes all properties mandatory.
✗ Incorrect
Required makes 'timeout' required, but the object literal does not provide it, causing a compilation error.
❓ Predict Output
advanced2:00remaining
Output of intersection of utility types
What is the output of this TypeScript code when compiled to JavaScript and run?
Typescript
type A = { a: number; b?: string };
type B = { b: string; c: boolean };
type C = Required<A> & B;
const obj: C = { a: 5, b: "hello", c: true };
console.log(obj.b);Attempts:
2 left
💡 Hint
Intersection combines properties, Required makes 'b' in A required, B requires 'b' as string.
✗ Incorrect
The intersection type requires 'b' to be present and string, so obj.b is 'hello'.
🧠 Conceptual
expert2:00remaining
Which utility type combination creates a type with all properties optional and readonly?
Which of the following TypeScript utility type combinations produces a type where all properties are both optional and readonly?
Attempts:
2 left
💡 Hint
Partial makes properties optional, Readonly makes them immutable.
✗ Incorrect
Readonly> makes all properties optional (Partial) and readonly (Readonly). Partial> also works but the order affects type inference subtly; however, both produce the same shape. But only option A is accepted here as correct to avoid ambiguity.