[TypeScript] Modifier
2023-09-14 09:00:48 时间
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] Write clean Type 2 - Reduce the usage in generic slot
- [Typescript] Generics in a Class Names Creator
- [Typescript] TypeScript module Augmentation
- [Typescript] Recursion Type
- [Typescript] Simplify iteration of custom data structures in TypeScript with iterators (backwards iteration with for ... of.. loop)
- [Webpack + React] Import CSS Modules with TypeScript and webpack
- [Typescript] Handling conditional return type of a function
- [Typescript] 137. Hard - Discriminated union to Object
- [Typescript] export = and import = require()
- [Typescript] 42. Medium - Remove Index Signature
- [Typescript] Tips: Use 'in' operator to transform a union to another union(watched)
- [Typescript] 14. Easy - Parameters
- [Typescript] Type JQuery
- [Vue + TS] Write a Vue Component as a Class in TypeScript
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
- TypeScript constructor signature 类型的变量赋值方式
- 使用 TypeScript 自定义装饰器给类的方法增添监听器 Listener