ReadonlyPerson after applying the mapped type with readonly modifier?type Person = { name: string; age: number };
type ReadonlyPerson = {
readonly [K in keyof Person]: Person[K];
};
const p: ReadonlyPerson = { name: "Alice", age: 30 };
p.name = "Bob"; // What happens here?The readonly modifier in a mapped type makes all properties read-only. Trying to assign a new value to p.name causes a compile-time error.
PartialPerson?type Person = { name: string; age: number };
type PartialPerson = {
[K in keyof Person]?: Person[K];
};
const p: PartialPerson = { name: "Alice" };The ? in the mapped type makes all properties optional, so both name and age become optional with their original types.
type Flags = { a: boolean; b: boolean };
type ModifiedFlags = {
+readonly [K in keyof Flags]-?: Flags[K];
};Option A is invalid because it uses a semicolon instead of a colon between the property name and type, causing a syntax error.
ReadonlyPartial?type User = { id: number; name: string };
type ReadonlyPartial = {
readonly [K in keyof User]?: User[K];
};The mapped type applies readonly and ? to each property, making them read-only and optional.
-readonly and -? modifiers do in the mapped type?type Config = { readonly host?: string; readonly port?: number };
type MutableRequiredConfig = {
-readonly [K in keyof Config]-?: Config[K];
};The -readonly removes the readonly modifier, and -? removes the optional modifier, so all properties become mutable and required.
