Challenge - 5 Problems
Pick Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);Attempts:
2 left
💡 Hint
Pick creates a new type by selecting only the specified keys from the original type.
✗ Incorrect
The Pick type creates a new type with only the keys 'name' and 'email' from User. So the object 'picked' must have only those two properties. The console.log outputs the object with those properties.
🧠 Conceptual
intermediate1:30remaining
Which statement about Pick type is true?
Choose the correct statement about the TypeScript Pick utility type.
Attempts:
2 left
💡 Hint
Think about what Pick does to the keys of a type.
✗ Incorrect
Pick creates a new type by selecting only the keys you specify from the original type. It does not exclude keys or modify the original type.
🔧 Debug
advanced2: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 };Attempts:
2 left
💡 Hint
Check if all keys in Pick exist in the original interface.
✗ Incorrect
The key "stock" does not exist in Product, so Pick is invalid and causes a type error.
📝 Syntax
advanced2: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;
}Attempts:
2 left
💡 Hint
Pick takes two parameters: the type and a union of keys as string literals.
✗ Incorrect
Pick expects the second parameter to be a union of keys as string literals separated by |, not multiple parameters or arrays or objects.
🚀 Application
expert2: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>;Attempts:
2 left
💡 Hint
keyof Settings returns all keys of Settings.
✗ Incorrect
keyof Settings returns all keys: "theme", "notifications", "autoSave", "version". Pick with all keys returns a type with all 4 keys.