Mutable
介绍
实现一个通用的类型 Mutable<T>
,使类型 T
的全部属性可变(非只读)。
ts
interface Todo {readonly title: stringreadonly description: stringreadonly completed: boolean}type MutableTodo = Mutable<Todo> // { title: string; description: string; completed: boolean; }
View on GitHubts
interface Todo {readonly title: stringreadonly description: stringreadonly completed: boolean}type MutableTodo = Mutable<Todo> // { title: string; description: string; completed: boolean; }
起点
ts
/* _____________ Your Code Here _____________ */typeMutable <T > = any/* _____________ Test Cases _____________ */interfaceTodo1 {title : stringdescription : stringcompleted : booleanmeta : {author : string}}typeList = [1, 2, 3]typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Mutable <Readonly <Todo1 >>,Todo1 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Mutable <Readonly <List >>,List >>,]
take the challengets
/* _____________ Your Code Here _____________ */typeMutable <T > = any/* _____________ Test Cases _____________ */interfaceTodo1 {title : stringdescription : stringcompleted : booleanmeta : {author : string}}typeList = [1, 2, 3]typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Mutable <Readonly <Todo1 >>,Todo1 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Mutable <Readonly <List >>,List >>,]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeMutable <T > = {-readonly [K in keyofT ]:T [K ]}
ts
// most populartypeMutable <T > = {-readonly [K in keyofT ]:T [K ]}