Removing modifiers with minus in Typescript - Time & Space Complexity
We want to understand how fast the program runs when we remove modifiers using the minus sign in TypeScript.
How does the time to remove modifiers change as the input grows?
Analyze the time complexity of the following code snippet.
type RemoveReadonly<T> = {
-readonly [P in keyof T]: T[P]
}
const example: RemoveReadonly<{ readonly a: number; readonly b: string }> = {
a: 5,
b: 'hello'
}
This code removes the readonly modifier from all properties of a type.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Iterating over each property key in the type
T. - How many times: Once for each property in the input type.
As the number of properties increases, the time to remove modifiers grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 property removals |
| 100 | 100 property removals |
| 1000 | 1000 property removals |
Pattern observation: Doubling the number of properties roughly doubles the work done.
Time Complexity: O(n)
This means the time to remove modifiers grows directly with the number of properties.
[X] Wrong: "Removing modifiers happens instantly no matter how many properties there are."
[OK] Correct: Each property must be processed, so more properties mean more work.
Understanding how type transformations scale helps you write efficient and clear TypeScript code, a skill valued in many coding tasks.
"What if we removed multiple modifiers at once? How would the time complexity change?"
Practice
What does the minus sign - do in TypeScript mapped types when used with modifiers like readonly or ??
Solution
Step 1: Understand mapped types modifiers
In TypeScript, modifiers likereadonlyand?control property mutability and optionality.Step 2: Role of minus sign in mapped types
Using-before a modifier removes that modifier from the properties in the new type.Final Answer:
It removes the specified modifier from the properties. -> Option BQuick Check:
Minus sign removes modifiers [OK]
- Thinking minus adds modifiers instead of removing
- Confusing minus with negation in values
- Assuming minus duplicates properties
Which of the following is the correct syntax to remove the readonly modifier from all properties of type T in a mapped type?
type Mutable<T> = { [P in keyof T]: T[P]}Solution
Step 1: Syntax for removing readonly modifier
In mapped types,-readonlyplaced before the property name removes the readonly modifier.Step 2: Check each option
type Mutable<T> = { -readonly [P in keyof T]: T[P] } correctly uses-readonly [P in keyof T]. Others misuse syntax or place minus incorrectly.Final Answer:
type Mutable<T> = { -readonly [P in keyof T]: T[P] } -> Option AQuick Check:
Minus before readonly removes it [OK]
- Placing minus after readonly
- Putting minus inside property type
- Using minus without readonly keyword
Given the following code, what is the type of Result?
type Original = { readonly a: number; b?: string; c: boolean };
type Result = { -readonly [K in keyof Original]-?: Original[K] };Solution
Step 1: Analyze modifiers removed
The-readonlyremoves readonly from all properties. The-?removes optional modifier, making all properties required.Step 2: Apply changes to Original type
Propertyaloses readonly,bloses optional, sobbecomes required string,cstays boolean required.Final Answer:
{ a: number; b: string; c: boolean } -> Option DQuick Check:
Minus removes readonly and optional [OK]
- Forgetting that -? removes optional
- Assuming readonly stays
- Thinking optional remains
Identify the error in this mapped type that tries to remove the ? optional modifier:
type RequiredProps<T> = { [P in keyof T]-?: T[P] }Which statement is true?
Solution
Step 1: Check syntax for removing optional modifier
In mapped types, placing-?immediately after the property name removes the optional modifier.Step 2: Verify given code
The code uses[P in keyof T]-?: T[P], which is correct syntax to remove optional.Final Answer:
The syntax is correct; it removes the optional modifier. -> Option CQuick Check:
Minus after property removes optional [OK]
- Placing minus before property name for optional
- Thinking minus can't remove optional
- Misplacing minus sign
You have a type with some properties readonly and some optional:
type Data = { readonly id: number; name?: string; active: boolean };Write a mapped type CleanData that removes both readonly and ? modifiers from all properties.
Which of these is correct?
Solution
Step 1: Remove readonly modifier
Use-readonlybefore the property name to remove readonly.Step 2: Remove optional modifier
Use-?immediately after the property name to remove optional.Step 3: Combine both in mapped type
Correct syntax is{ -readonly [P in keyof Data]-?: Data[P] }.Final Answer:
type CleanData = { -readonly [P in keyof Data]-?: Data[P] } -> Option AQuick Check:
Minus readonly before and minus ? after property [OK]
- Placing minus signs incorrectly
- Trying to put minus signs inside brackets
- Using spaces that break syntax
Ready to prove it?
Pass the test and this pattern moves unknown
