0
0
Typescriptprogramming~20 mins

Pick type in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pick Type 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 Pick type example?
Consider the following TypeScript code using the Pick utility type. What is the type of picked and what will be logged?
Typescript
interface User {
  id: number;
  name: string;
  age: number;
  email: string;
}

const user: User = { id: 1, name: "Alice", age: 30, email: "alice@example.com" };

type UserNameAndEmail = Pick<User, "name" | "email">;

const picked: UserNameAndEmail = { name: "Alice", email: "alice@example.com" };

console.log(picked);
A{"age": 30, "email": "alice@example.com"}
B{"id": 1, "name": "Alice"}
CTypeScript compilation error due to missing properties
D{"name": "Alice", "email": "alice@example.com"}
Attempts:
2 left
💡 Hint
Pick creates a new type by selecting only the specified keys from the original type.
🧠 Conceptual
intermediate
1:30remaining
Which statement about Pick type is true?
Choose the correct statement about the TypeScript Pick utility type.
APick creates a new type by excluding specified keys from the original type.
BPick can only be used with primitive types, not interfaces.
CPick creates a new type by selecting specified keys from the original type.
DPick modifies the original type by adding new keys.
Attempts:
2 left
💡 Hint
Think about what Pick does to the keys of a type.
🔧 Debug
advanced
2:30remaining
Why does this Pick type usage cause an error?
Given this code, why does TypeScript raise an error?
Typescript
interface Product {
  id: number;
  name: string;
  price: number;
}

type ProductNameAndStock = Pick<Product, "name" | "stock">;

const product: ProductNameAndStock = { name: "Book", stock: 10 };
A"stock" is not a key of Product, so Pick causes a type error.
BThe object is missing the "price" property, causing an error.
CPick cannot be used with more than one key.
DThe object must include all keys of Product, Pick does not change that.
Attempts:
2 left
💡 Hint
Check if all keys in Pick exist in the original interface.
📝 Syntax
advanced
2:00remaining
Which option correctly uses Pick to select keys?
Select the correct TypeScript syntax to create a type with only the "title" and "author" keys from the interface Book.
Typescript
interface Book {
  title: string;
  author: string;
  pages: number;
  published: Date;
}
Atype BookSummary = Pick<Book, "title", "author">;
Btype BookSummary = Pick<Book, "title" | "author">;
Ctype BookSummary = Pick<Book, ["title", "author"]>;
Dtype BookSummary = Pick<Book, { title: string; author: string }>;
Attempts:
2 left
💡 Hint
Pick takes two parameters: the type and a union of keys as string literals.
🚀 Application
expert
2:00remaining
How many keys are in the resulting type?
Given this code, how many keys does the type PartialPick have?
Typescript
interface Settings {
  theme: string;
  notifications: boolean;
  autoSave: boolean;
  version: number;
}

type PartialPick = Pick<Settings, keyof Settings>;
A4 keys
B3 keys
C2 keys
D0 keys
Attempts:
2 left
💡 Hint
keyof Settings returns all keys of Settings.