Sometimes you want to take a type and remove certain special words (modifiers) from its properties. This helps make the type simpler or change how it behaves.
Removing modifiers with minus in Typescript
type NewType = { -readonly [K in keyof OldType]: OldType[K] };
type AnotherType = { -? [K in keyof OldType]: OldType[K] };The minus sign - before readonly or ? removes that modifier from the type.
This syntax is used inside mapped types to change property modifiers.
readonly and ? (optional) from all properties, making them writable and required.type ReadonlyUser = {
readonly name: string;
readonly age?: number;
};
type WritableUser = {
-readonly [K in keyof ReadonlyUser]-?: ReadonlyUser[K];
};? optional modifier, making all properties required.type OptionalUser = {
name: string;
age?: number;
};
type RequiredUser = {
[K in keyof OptionalUser]-?: OptionalUser[K];
};readonly modifier, keeping optional properties as they are.type ReadonlyUser = {
readonly name: string;
age: number;
};
type WritableUser = {
-readonly [K in keyof ReadonlyUser]: ReadonlyUser[K];
};This example shows how to remove both readonly and ? from properties. The new type EditablePerson has writable and required properties.
type ReadonlyPerson = {
readonly id: number;
readonly name?: string;
};
type EditablePerson = {
-readonly [K in keyof ReadonlyPerson]-?: ReadonlyPerson[K];
};
const person: EditablePerson = {
id: 1,
name: "Alice"
};
// person.id = 2; // This works because 'readonly' is removed
// person.name = undefined; // Not allowed because '?' is removed
console.log(person);Removing modifiers only works inside mapped types using [K in keyof T] syntax.
Using -readonly removes the readonly modifier, making properties writable.
Using -? removes the optional modifier, making properties required.
You can remove readonly and ? modifiers using a minus sign - in mapped types.
This helps create new types with different property rules based on existing types.
It is useful for controlling mutability and optionality in your TypeScript types.
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
