Satisfies operator in Typescript - Time & Space Complexity
We want to see how the time it takes to check if a value meets a type condition changes as the input grows.
How does using the satisfies operator affect the work done when checking types?
Analyze the time complexity of the following code snippet.
const colors = ["red", "green", "blue"] as const;
const color = "red" satisfies (typeof colors)[number];
const bigArray = Array(1000).fill("red") as const;
const check = bigArray.every(c => (c satisfies (typeof colors)[number], true));
This code uses the satisfies operator to check if values match a specific type from a list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
everymethod loops over each item inbigArrayto check the type withsatisfies. - How many times: It runs once for each element, so 1000 times in this example.
Each new item in the array adds one more check using the satisfies operator.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 type checks |
| 100 | 100 type checks |
| 1000 | 1000 type checks |
Pattern observation: The number of operations grows directly with the number of items checked.
Time Complexity: O(n)
This means the time to check all items grows in a straight line as the list gets bigger.
[X] Wrong: "The satisfies operator runs instantly no matter how many items there are."
[OK] Correct: Each item must be checked one by one, so more items mean more work.
Understanding how type checks scale helps you write efficient code and explain your reasoning clearly in interviews.
"What if we replaced the array with a Set? How would the time complexity of checking all items change?"