Extract type in Typescript - Time & Space Complexity
Let's see how the time it takes to run code using the Extract type changes as the input grows.
We want to know how the work done scales when extracting types from bigger sets.
Analyze the time complexity of the following code snippet.
type Extract<T, U> = T extends U ? T : never;
// Example usage:
type Result = Extract<'a' | 'b' | 'c', 'a' | 'c'>;
// Result is 'a' | 'c'
This code picks types from T that are assignable to U, filtering the union types.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each member of the union type T against U.
- How many times: Once for each member in the union T.
Each additional type in T means one more check against U.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of types in T.
Time Complexity: O(n)
This means the time to extract types grows linearly with the number of types in the input.
[X] Wrong: "Extract type runs instantly no matter how many types are in the union."
[OK] Correct: Each type in the union must be checked, so more types mean more work.
Understanding how type operations scale helps you reason about code performance and complexity in real projects.
"What if we changed the Extract type to check against multiple types in U? How would the time complexity change?"
Practice
Extract<T, U> utility type do?Solution
Step 1: Understand the purpose of Extract
Extract<T, U>picks fromTonly those types that can be assigned toU.Step 2: Compare with other utility types
UnlikeExclude<T, U>which removes types,Extractkeeps matching types.Final Answer:
It creates a new type by picking types fromTthat are assignable toU. -> Option CQuick Check:
Extract = picks matching types [OK]
- Confusing Extract with Exclude
- Thinking Extract combines types
- Assuming Extract converts types to strings
type U = string | number | boolean?Solution
Step 1: Identify the correct order of parameters
Extract<T, U>takes the big unionTfirst, then the type to pickU.Step 2: Match the desired extracted type
We want to pickstringfromU = string | number | boolean, soExtract<U, string>is correct.Final Answer:
type OnlyString = Extract<U, string>; -> Option AQuick Check:
Extract<Union, PickType> syntax [OK]
- Swapping the order of parameters
- Picking wrong type in second parameter
- Using Extract with unrelated types
type Mixed = string | number | boolean;
type Result = Extract;
What is the type of
Result?Solution
Step 1: Understand the union types involved
Mixedisstring | number | boolean. We want to extract types assignable tonumber | boolean.Step 2: Extract matching types
FromMixed, onlynumberandbooleanmatchnumber | boolean.stringis excluded.Final Answer:
number | boolean -> Option DQuick Check:
Extract picks matching types [OK]
- Including types not in second parameter
- Confusing Extract with union
- Thinking Extract returns never if partial match
type A = string | number;
type B = Extract;
What will be the type of
B?Solution
Step 1: Analyze the Extract parameters
Aisstring | number, and we try to extractbooleantypes.Step 2: Determine matching types
Sincebooleanis not assignable tostringornumber, no types match, so the result isnever.Final Answer:
never -> Option BQuick Check:
No matching types means never [OK]
- Expecting an error instead of never
- Assuming Extract returns original union
- Confusing Extract with Exclude
type Data = string | number | null | undefined. You want to create a type that excludes null and undefined but only keeps string and number. Which Extract usage achieves this?Solution
Step 1: Identify the types to keep
We want to keep onlystringandnumber, excludingnullandundefined.Step 2: Use Extract with correct second parameter
UsingExtract<Data, string | number>picks onlystringandnumberfromData.Final Answer:
type CleanData = Extract<Data, string | number>; -> Option AQuick Check:
Extract filters union by matching types [OK]
- Including null or undefined in second param
- Using Exclude instead of Extract
- Confusing Extract with union
Ready to prove it?
Pass the test and this pattern moves unknown
