typescriptMapped Types
type Person = {
name: string;
age: number;
location: string
}
// 1、Mapped Types 只匹配泛型的key,自定义value类型
type OptionFlag<T> = {
[Property in keyof T]: boolean
}
const t1:OptionFlag<Person> = {
name: false,
age: false,
location: false
}
// 2、Mapping Modifiers 匹配修饰符
type PersonReadonly = {
readonly name: string;
readonly age: number;
readonly location?: string
}
// remove readonly: location非必填
type OptionFlagRemoveReadonly<T> = {
-readonly [Property in keyof T] : T[Property]
}
const optionFlagRemoveReadonly: OptionFlagRemoveReadonly<PersonReadonly> = {
name: "name",
age: 11,
}
// remove options:location必填
type OptionFlagRemoveReadonly2<T> = {
-readonly [Property in keyof T]-?: T[Property]
}
const optionFlagRemoveReadonly2: OptionFlagRemoveReadonly2<PersonReadonly> = {
name: "name",
age: 11,
location: 'uuu'
}
// 3、Key Remapping via as 重新匹配key
// Capitalize 大写
type Getter<T> = {
[Property in keyof T as `get${Capitalize<string & Property>}`]: T[Property]
}
const tt:Getter<Person> = {
getName: "11",
getAge: 11,
getLocation: "11"
}
// Remove the 'kind' property
type RemovePersonName<T>= {
[Property in keyof T as Exclude<Property, 'name'>] : T[Property]
}
const removePersonName:RemovePersonName<Person> = {
age: 11,
location: 'ss'
}
// function
type EventConfig<Events extends { kind: string }> = {
[E in Events as E["kind"]]: (event: E) => void;
}
type SquareEvent = { kind: "square", x: number, y: number };
type CircleEvent = { kind: "circle", radius: number };
type Config = EventConfig<SquareEvent | CircleEvent>
type EventConfig2<Events extends {kind: string}> = {
[E in Events as E['kind']] : (event:E)=> void
}
const a:EventConfig2<SquareEvent | CircleEvent> = {
square: ({kind, x, y})=>{
console.log("square", {kind, x, y})
},
circle: ({kind, radius})=>{
console.log("circle", {kind, radius})
}
}
a.square({kind: "square", x: 0, y: 9})
a.circle({kind: "circle", radius: 9})
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhihbfab
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
excel下划线不显示怎么办
PHP中文网 06-23 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel图片置于文字下方的方法
PHP中文网 06-27 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22