Mutable

Challenge

Implement the generic Mutable<T> which makes all properties in T mutable (not readonly).

For example

interface Todo {
  readonly title: string;
  readonly description: string;
  readonly completed: boolean;
}

type MutableTodo = Mutable<Todo>; // { title: string; description: string; completed: boolean; }

Solution

Zur Lösung dieses Problems müssen wir lediglich einen Mapped Type erstellen, welcher den Mapped Type Modifier -readonly nutzt, um einen Modifier zu entfernen.

type Mutable<T extends Record<string, any>> = {
  -readonly [Key in keyof T]: T[Key];
};

References