标签:dma inter void rdma 工具 获取对象 uva partial eof
interface Bird { name: string, fly(): void } interface Person { age: number, talk(): void; } type BirdMan = Bird & Person; let bm: BirdMan = {name: ‘lee‘, fly() {}, age: 1, talk(){}}
let user = {name: ‘lee‘, age: 1, talk() {}}; type User = typeof user; // 相当于 {name: string, age: number, talk():void}
type Parent = { name: string, son: { name: string, age: number } } // 注意不能使用.运算符 let son: Parent[‘son‘] = {name: ‘lee‘, age: 1};
type Stu = { score: number, name: string } type StuKey = keyof Stu; // ‘score‘|‘name‘ function getStuValue(stu: Stu, key: StuKey) { return stu[key] } let stu = {score: 90, name: ‘lee‘}; // ? Argument of type ‘"ddd"‘ is not assignable to parameter of type ‘"score" | "name"‘. getStuValue(stu, ‘ddd‘);
type Person1 = { name: string, age: number, gender: boolean } //PersonMap是从Person1映射的类型,该类型各属性都变为可选 type PersonMap = { [key in keyof Person1]?: Person1[key] } let p1:PersonMap = {name: ‘lee‘};
所有的工具的第一个参数为具体的类型;可以是以下类型(interface, enum, class, type)
// 原理 type Partival<T> = {[key in keyof T]+?: T[key]};
示例:
interface PersonA { user: string } type NewPersonA = Partial<Person>; // {user?: string}
// 原理 type Required<T> = {[key in keyof T]-?: T[key]}
示例:
class PersonB { name?: string } type NewPersonB = Required<PersonB>; // 测试用例 let perB: NewPersonB = {name: ‘lee‘};
// 原理: type Readonly<T> = {readonly [key in keyof T]: T[key]}
示例:
type PersonC = { age: number } type NewPersonC = Readonly<PersonC>; let perC: NewPersonC = {age: 10}; perC.age = 12; // ?
// 原理: type Pick<T, Key extends keyof T> = {[key in Key]: T[key]}
示例:
interface PersonD { gender: boolean, intersets: "ball" | "sing", age: number } type NewColor = Pick<PersonD, ‘gender‘|‘age‘>; let newC: NewColor = {gender: true, age: 10};
标签:dma inter void rdma 工具 获取对象 uva partial eof
原文地址:https://www.cnblogs.com/lyraLee/p/12398208.html