CodePark/ questions / Mapped & template literal types
START PRACTICINGPRACTICE
Frontend question · TypeScript › Mapped & template literal types

What is U?

type Mutable<T> = { -readonly [K in keyof T]-?: T[K] };
type U = Mutable<{ readonly id: string; nickname?: string }>;
{ id: string; nickname: string }

The answer is { id: string; nickname: string }. -readonly removes readonly, and -? removes the optional ?. Removing ? also drops the undefined that the ? added. So nickname is plain string, the same behaviour as Required.

{ id: string; nickname: string | undefined } is the tempting wrong answer, because it keeps that undefined.