Typescript類型體操 - Mutable

題目

中文

實現一個通用的類型 Mutable<T>,使類型 T 的全部屬性可變(非只讀)。

例如:

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

English

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; }

答案

type Mutable<T extends object> = { -readonly [K in keyof T]: T[K] };

在線演示

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章