码迷,mamicode.com
首页 > 其他好文 > 详细

[TypeScript] Modifier

时间:2019-10-17 19:17:22      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:span   spec   types   mod   ble   ext   readonly   esc   rem   

TypeScript 2.8 adds the ability for a mapped type to either add or remove a particular modifier. Specifically, a readonly or ? property modifier in a mapped type can now be prefixed with either + or - to indicate that the modifier should be added or removed.

type MutableRequired<T> = { -readonly [P in keyof T]-?: T[P] };  // Remove readonly and ?
type ReadonlyPartial<T> = { +readonly [P in keyof T]+?: T[P] };  // Add readonly and ?

 

Example:

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

interface Book { 
    readonly name: String;
}

const newState: MutableRequired<Book> = {
    name: st
};

newState‘s anme is mutable and required.

 

[TypeScript] Modifier

标签:span   spec   types   mod   ble   ext   readonly   esc   rem   

原文地址:https://www.cnblogs.com/Answer1215/p/11694123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!