Partial type in Typescript - Time & Space Complexity
Let's explore how the time it takes to create a partial type grows as the size of the original type grows.
We want to know how the work changes when we make some or all properties optional.
Analyze the time complexity of the following code snippet.
type User = {
id: number;
name: string;
email: string;
age: number;
};
type PartialUser = Partial;
This code creates a new type where all properties of User become optional.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Iterating over each property of the original type.
- How many times: Once for each property in the type.
As the number of properties increases, the work to make them optional grows linearly.
| Input Size (number of properties) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The work grows directly with the number of properties.
Time Complexity: O(n)
This means the time to create a partial type grows in a straight line with the number of properties.
[X] Wrong: "Making a partial type is instant and does not depend on the number of properties."
[OK] Correct: Each property must be processed to become optional, so more properties mean more work.
Understanding how type transformations scale helps you reason about code maintainability and compiler work behind the scenes.
"What if we used a nested type with properties that are themselves objects? How would the time complexity change?"
Practice
What does the Partial utility type do in TypeScript?
Solution
Step 1: Understand the purpose of Partial
ThePartialtype is designed to take an existing type and make all its properties optional.Step 2: Compare with other utility types
UnlikeRequiredwhich makes all properties mandatory,Partialdoes the opposite by making them optional.Final Answer:
Makes all properties of a type optional -> Option DQuick Check:
Partial = optional properties [OK]
- Confusing Partial with Required
- Thinking Partial removes properties
- Assuming Partial makes properties readonly
Which of the following is the correct syntax to create a partial type of User?
interface User { name: string; age: number; }Solution
Step 1: Recall correct Partial syntax
The correct way to create a partial type is usingPartial<Type>syntax.Step 2: Check each option
type PartialUser = Partial<User>; uses correct syntax. Options A, B, and D have invalid or incorrect TypeScript syntax.Final Answer:
type PartialUser = Partial<User>; -> Option AQuick Check:
Partial syntax = Partial<Type> [OK]
- Using dot notation instead of generic syntax
- Adding extra equals or braces incorrectly
- Confusing Partial with object literals
What is the output of this TypeScript code?
interface Product { id: number; name: string; price: number; }
const updateProduct: Partial<Product> = { price: 99 };
console.log(updateProduct);Solution
Step 1: Understand Partial effect on properties
Partial makes all properties optional, so you can provide only some properties.Step 2: Check the object assigned
The object has onlyprice: 99. This is valid and prints exactly that.Final Answer:
{ price: 99 } -> Option BQuick Check:
Partial allows partial objects [OK]
- Expecting all properties to appear with undefined
- Thinking missing properties cause errors
- Assuming Partial creates empty objects
Identify the error in this code snippet:
interface Settings { theme: string; fontSize: number; }
const newSettings: Partial<Settings> = { theme: 'dark' };
newSettings.fontSize.toFixed(2);Solution
Step 1: Analyze Partial type effect
Partial makes all properties optional, sofontSizemay be undefined.Step 2: Check method call on possibly undefined
CallingtoFixedon possibly undefinedfontSizecauses a runtime error.Final Answer:
Error: fontSize might be undefined -> Option CQuick Check:
Partial properties can be undefined [OK]
- Ignoring optional property undefined possibility
- Assuming all properties exist
- Confusing method errors with type errors
You have this interface and partial update function:
interface Profile { username: string; email: string; age: number; }
function updateProfile(profile: Profile, updates: Partial<Profile>): Profile {
return { ...profile, ...updates };
}What is the result of calling updateProfile({ username: 'alice', email: 'a@example.com', age: 30 }, { email: 'alice@new.com' })?
Solution
Step 1: Understand spread with Partial
The function merges the original profile with updates, overwriting provided fields.Step 2: Apply the update
Theemailfield is replaced with the new value, others remain unchanged.Final Answer:
{ username: 'alice', email: 'alice@new.com', age: 30 } -> Option AQuick Check:
Spread merges updates over original [OK]
- Thinking Partial cannot be spread
- Expecting original email to remain
- Returning only updates without original fields
Ready to prove it?
Pass the test and this pattern moves unknown
