Exclude type in Typescript - Time & Space Complexity
Let's see how the time needed to run code changes when we use the Exclude type in TypeScript.
We want to know how the work grows as the list of types gets bigger.
Analyze the time complexity of the following code snippet.
type Exclude<T, U> = T extends U ? never : T;
// Example usage:
type Result = Exclude<'a' | 'b' | 'c', 'a' | 'b'>;
// Result is 'c'
This code removes types from T that are also in U, leaving only types not in U.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each type in T against all types in U.
- How many times: For each type in T, it compares with types in U.
As the number of types in T and U grows, the checks increase.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 checks |
| 100 | About 10,000 checks |
| 1000 | About 1,000,000 checks |
Pattern observation: The work grows quickly as both lists get bigger, multiplying together.
Time Complexity: O(n * m)
This means the time grows by multiplying the size of the first list by the size of the second list.
[X] Wrong: "The Exclude type only checks each type once, so it is O(n)."
[OK] Correct: It actually compares each type in T with every type in U, so the work multiplies.
Understanding how type operations scale helps you reason about code performance and complexity in real projects.
What if we changed U to be a single type instead of a union? How would the time complexity change?
Practice
Exclude utility type do?Solution
Step 1: Understand the purpose of
ExcludeExcludeis designed to remove certain types from a union type.Step 2: Compare with other utility types
Unlike adding or converting types,Excludespecifically filters out unwanted types.Final Answer:
Removes specific types from a union type -> Option CQuick Check:
Exclude= Remove types [OK]
- Thinking it adds types instead of removing
- Confusing with Partial or Readonly utility types
- Assuming it converts types to strings
'b' from the union 'a' | 'b' | 'c'?Solution
Step 1: Check the syntax of
The correct syntax isExcludeExclude<UnionType, ExcludedType>.Step 2: Match the correct exclusion
type Result = Exclude<'a' | 'b' | 'c', 'b'>; correctly excludes'b'from the union'a' | 'b' | 'c'.Final Answer:
type Result = Exclude<'a' | 'b' | 'c', 'b'>; -> Option BQuick Check:
Exclude syntax = Exclude<Union, TypeToRemove> [OK]
- Using array syntax for second parameter
- Omitting the second parameter
- Excluding a type not in the union
type Letters = 'x' | 'y' | 'z'; type Result = Exclude;
Solution
Step 1: Identify the original union type
Lettersis'x' | 'y' | 'z'.Step 2: Apply
RemovingExcludeto remove'y'and'z''y'and'z'leaves only'x'.Final Answer:
'x' -> Option AQuick Check:
Exclude removes 'y' and 'z', leaving 'x' [OK]
- Thinking it keeps excluded types
- Confusing with intersection types
- Assuming result is never when some types remain
type Colors = 'red' | 'green' | 'blue'; type NoGreen = Exclude<Colors>;
Solution
Step 1: Check the
ExcludesyntaxExcluderequires two parameters: the union type and the type(s) to exclude.Step 2: Identify the missing parameter
The code only provides one parameter, missing the second one to exclude.Final Answer:
Missing second parameter in Exclude -> Option AQuick Check:
Exclude needs two types: Union and Excluded [OK]
- Forgetting the second parameter
- Assuming Exclude works with one argument
- Confusing Exclude with other utility types
Filtered?type Status = 'success' | 'error' | 'pending' | 'cancelled'; type Filtered = Exclude;
Solution
Step 1: Identify the original union and excluded types
Statusis'success' | 'error' | 'pending' | 'cancelled'. We exclude'error','cancelled', and'unknown'.Step 2: Remove excluded types from the union
'error'and'cancelled'are removed.'unknown'is not inStatus, so it has no effect.Final Answer:
'success' | 'pending' -> Option DQuick Check:
Exclude removes only matching types from union [OK]
- Including excluded types in the result
- Adding excluded types not in the union
- Assuming result is never if unknown type excluded
Ready to prove it?
Pass the test and this pattern moves unknown
