0
0
Typescriptprogramming~20 mins

Combining utility types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Combining Utility Types
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A{ id: 1, name: undefined, email: undefined }
B{ id: 1 }
CTypeError at runtime
DCompilation error due to type mismatch
Attempts:
2 left
💡 Hint
Partial makes all properties optional, Readonly makes them immutable.
Predict Output
intermediate
2: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'>>;
Aid and price, both optional
Bid, price, and name, all required
Cid and price, both required
DOnly id required, price optional
Attempts:
2 left
💡 Hint
Pick selects keys, Required makes them non-optional.
🔧 Debug
advanced
2: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"
};
AType 'string' is not assignable to type 'number' for timeout
BReadonly does not allow object literals
CNo error, code compiles fine
DMissing required property 'timeout' in object literal
Attempts:
2 left
💡 Hint
Required makes all properties mandatory.
Predict Output
advanced
2: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);
Ahello
Bundefined
CTypeError at runtime
DCompilation error due to conflicting types
Attempts:
2 left
💡 Hint
Intersection combines properties, Required makes 'b' in A required, B requires 'b' as string.
🧠 Conceptual
expert
2: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?
AReadonly<Partial<T>>
BPartial<Readonly<T>>
CRequired<Readonly<T>>
DReadonly<Required<T>>
Attempts:
2 left
💡 Hint
Partial makes properties optional, Readonly makes them immutable.