Q33: How do Mapping Modifiers (`+` and `-`) affect `readonly` and optional (`?`) attributes?
Mapped types can add or remove property modifiers using + (default if omitted) or - prefixes.
-readonly: Removesreadonlystatus from fields (makes fields mutable).-?: Removes optional status from fields (makes fields required).
Stripping Readonly and Optional Modifiers
type MutableAndRequired<T> = {
-readonly [K in keyof T]-?: T[K];
};
interface Locked {
readonly id?: string;
}
type Unlocked = MutableAndRequired<Locked>; // { id: string; }💡 -readonly strips immutability, while -? strips optional flags.
Key Takeaways
- Use
-readonlyand-?to strip modifiers from mapped properties.