Pick type in Typescript - Time & Space Complexity
Let's see how the time it takes to create a new type using Pick changes as the number of keys grows.
We want to know how the work grows when we pick more properties from a type.
Analyze the time complexity of this TypeScript type operation.
type Original = {
a: string;
b: number;
c: boolean;
d: Date;
e: string[];
};
type Picked = Pick;
This code creates a new type by selecting some keys from an existing type.
What happens behind the scenes when Pick runs?
- Primary operation: Iterating over the keys to select the chosen ones.
- How many times: Once for each key you pick.
As the number of keys to pick grows, the work grows too.
| Input Size (number of keys) | Approx. Operations |
|---|---|
| 3 | 3 checks |
| 10 | 10 checks |
| 100 | 100 checks |
Pattern observation: The work grows directly with the number of keys you pick.
Time Complexity: O(n)
This means the time to create the picked type grows linearly with the number of keys selected.
[X] Wrong: "Pick runs instantly no matter how many keys are selected."
[OK] Correct: Even though it's fast, Pick must check each key to include it, so more keys mean more work.
Understanding how type operations scale helps you reason about code maintainability and performance in larger projects.
What if we used a type that picks keys conditionally instead of explicitly? How would that affect time complexity?
Practice
Pick type do in TypeScript?Solution
Step 1: Understand the purpose of Pick
The Pick type is used to create a new type by choosing certain properties from an existing type.Step 2: Compare with other options
Removing properties is done by Omit, combining types by intersection, and making properties optional by Partial.Final Answer:
Creates a new type by selecting specific properties from an existing type. -> Option BQuick Check:
Pick selects properties = A [OK]
- Confusing Pick with Omit
- Thinking Pick makes properties optional
- Mixing Pick with type intersections
PersonName with only name and age properties from Person using Pick?Solution
Step 1: Recall Pick syntax
Pick takes two parameters: the original type and a union of property names as string literals.Step 2: Identify correct union syntax
The union of keys is written as 'name' | 'age' inside quotes and separated by |.Final Answer:
type PersonName = Pick; -> Option AQuick Check:
Pick syntax uses union of strings = B [OK]
- Omitting quotes around keys
- Using array syntax instead of union
- Passing keys as object instead of union
interface User { id: number; name: string; email: string; } and type UserPreview = Pick;, what is the type of userPreview in this code?const userPreview: UserPreview = { id: 1, name: 'Alice' };Solution
Step 1: Understand Pick selects properties
UserPreview picks only 'id' and 'name' from User, so it has only those two properties.Step 2: Match the type of userPreview
The object assigned has 'id' and 'name' only, matching the Pick type exactly.Final Answer:
{ id: number; name: string; } -> Option DQuick Check:
Pick selects only chosen keys = A [OK]
- Assuming all original properties remain
- Confusing Pick with Partial
- Including properties not picked
interface Product { id: number; name: string; price: number; }
type ProductInfo = Pick;Solution
Step 1: Check keys used in Pick
Pick requires keys to exist in the original type. 'cost' is not a key in Product.Step 2: Understand TypeScript error
Using a non-existent key causes a type error because Pick cannot select keys that don't exist.Final Answer:
'cost' is not a key of Product, causing a type error. -> Option CQuick Check:
Pick keys must exist in type = C [OK]
- Using keys not in the original type
- Thinking Pick keys can be arrays
- Assuming Pick works with any string keys
interface Employee { id: number; name: string; department: string; salary: number; }. You want to create a type EmployeeSummary that includes only id, name, and a new property departmentCode which is a string. How can you do this using Pick and adding the new property?Solution
Step 1: Use Pick to select existing keys
Pick 'id' and 'name' from Employee to get those properties only.Step 2: Add new property with intersection
Use & to add a new property 'departmentCode' with type string to the picked type.Final Answer:
type EmployeeSummary = Pick & { departmentCode: string }; -> Option AQuick Check:
Pick keys + add new property with & = D [OK]
- Trying to Pick a non-existent property
- Using Omit instead of Pick incorrectly
- Using Partial which makes all properties optional
Ready to prove it?
Pass the test and this pattern moves unknown
