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?"
Practice
What does the satisfies operator do in TypeScript?
Solution
Step 1: Understand the purpose of
Thesatisfiessatisfiesoperator checks if a value fits a type but keeps the original value unchanged.Step 2: Compare with other type operations
Unlike type conversion or creating new types,satisfiesonly verifies compatibility without altering the value.Final Answer:
Checks if a value matches a type without changing the value -> Option DQuick Check:
satisfies= type check only [OK]
- Thinking it converts or changes the value
- Confusing it with type casting
- Assuming it removes object properties
Which of the following is the correct syntax to use the satisfies operator in TypeScript?
const data = { name: "Alice", age: 30 } ___ Person;Solution
Step 1: Recall the syntax of
Thesatisfiessatisfiesoperator is used between the value and the type like:value satisfies Type.Step 2: Identify the correct operator in the code
In the example, the blank should be filled withsatisfiesto check the type without changingdata.Final Answer:
satisfies -> Option CQuick Check:
Syntax usessatisfieskeyword [OK]
- Using ':' which is for type annotations
- Using 'as' which is for type assertions
- Using 'implements' which is for classes
What will be the output of this TypeScript code?
type Person = { name: string; age: number };
const user = {
name: "Bob",
age: 25,
city: "Paris"
} satisfies Person;
console.log(user.city);Solution
Step 1: Understand what
Thesatisfiesdoes to the objectsatisfiesoperator checks thatusermatchesPersontype but keeps all original properties, includingcity.Step 2: Check the console.log output
Sinceuserstill hascity, logginguser.cityprints "Paris" without error.Final Answer:
"Paris" -> Option AQuick Check:
satisfieskeeps extra properties [OK]
- Assuming satisfies removes extra properties
- Expecting a type error on extra properties
- Confusing satisfies with type assertion
Identify the error in this TypeScript code using satisfies:
type Config = { port: number; host: string };
const serverConfig = {
port: "8080",
host: "localhost"
} satisfies Config;Solution
Step 1: Check the type of each property in
TheserverConfigportproperty is a string "8080" butConfigexpects a number.Step 2: Understand what
satisfiescheckssatisfiesverifies type compatibility and will raise a type error because of the wrong type forport.Final Answer:
Type error: 'port' should be a number, not a string -> Option AQuick Check:
Type mismatch causes error with satisfies [OK]
- Ignoring type mismatch on properties
- Thinking satisfies changes the value type
- Assuming no error if extra properties exist
You want to create a constant object settings that must have theme as a string and notifications as a boolean. You also want to keep extra properties without losing their types. Which code correctly uses satisfies to achieve this?
Solution
Step 1: Understand the goal
You wantsettingsto be checked against a type but keep extra properties likeversionwith their types intact.Step 2: Compare options
const settings = { theme: "dark", notifications: true, version: 2 } satisfies { theme: string; notifications: boolean }; usessatisfieswhich checks the required properties but keeps extra ones with full types. const settings: { theme: string; notifications: boolean } = { theme: "dark", notifications: true, version: 2 }; uses type annotation which drops extra properties. const settings = { theme: "dark", notifications: true, version: 2 } as { theme: string; notifications: boolean }; uses type assertion which forces the type and may lose extra property types. const settings = { theme: "dark", notifications: true, version: 2 }; has no type checking.Final Answer:
const settings = { theme: "dark", notifications: true, version: 2 } satisfies { theme: string; notifications: boolean }; -> Option BQuick Check:
satisfieskeeps extra properties typed [OK]
- Using type annotation and losing extra properties
- Using type assertion and ignoring type safety
- Not checking required properties at all
Ready to prove it?
Pass the test and this pattern moves unknown
