赞
踩
partial 将所有持有属性变为可选
interface data {
title: String,
desc: String,
say?: String
}
const testPartial = (dataOne: data, dataTwo: Partial<data>) => {
return {...dataOne, ...dataTwo}
}
console.log(testPartial({title: '测试1', desc: '描述1'}, {desc: '我只需要传入一个'}))
Required 与partial相反 将可选属性变为必选
const testRequired = (dataOne: data, dataTwo: Required<data>) => {
return {...dataOne, ...dataTwo}
}
console.log(testRequired({title: '测试1', desc: '描述1'}, {title: '测试2', desc: '我只需要传三个', say: '我在测试Required'}))
Readonly 所有属性只读
const testReadonly = (dataOne: data, dataTwo: Readonly<data>) => {
return {...dataOne, ...dataTwo}
}
console.log(testReadonly({title: '测试1', desc: '描述1'}, {title: '测试2', desc: '我是只读', say: '我在测试Readonly'}))
Record<keys, type> keys 代表键的类型 type代表值的类型
interface recordFace {
title: String
}
type keyValue = 'home' | 'about' | 'contact'
// type keyValue = string key只要是string就行
const testRecord: Record<keyValue, recordFace[]> = {
about: [{ title: 'about'}],
home: [{ title: 'home'}],
contact: [{ title: 'contact'}]
}
Omit<type, key> 从type仲移除key对应的属性
interface testOmit{
title: String,
desc: String
}
// type 定义类型
type testOmit2 = Omit<testOmit, 'desc'>
const todoOmit: testOmit2 = {
title: '我又来测试了',
}
Exclude<Type, ExcludedUnion> 在type中删除ExcludedUnion 取不同部分
Extract <Type, Union> 生成交集 取相同部分
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。