Jack Q
2024-05-27
来自广东
// A extends B type UNKS = '1' | '2' | '3' | 'aa'; type UNK = '1' | '2' | '3'; type KEYS = UNK extends UNKS ? UNK : never; // UNK type IsNeverArrayExtendsAnyArray = [] extends any[] ? true : false; type MyPick<T, K extends keyof T> = { [Key in keyof T & K]: T[Key] } // 作业2:单类型与表达式类型有什么不同 // 单类型可以是视作没有操作符的表达式类型, 所以最主要的不同点就是 有没有 操作符 // (第二部分)
Jack Q
2024-05-27
来自广东
// 作业1:六种表达式语法 const booleanVar = true; const numberVar = 1; const objectVar = { a: 1, b: '2', c: false, d: ['list'], f: { g: null } }; interface A { a: string, b: number, c: boolean, } type B = string[]; // Array<string> type C = [1, 'number', true]; // tunple type D = { d: string, e: number, f: true[] }; type F = A | B; // Unions type G = A & B; // Intersections type G1 = { g: string; h: null } & { i: boolean; j: number }; // Intersections enum H { l, m } enum I { m = '3', n = '4' } // typeof type bvT = typeof booleanVar; type nvT = typeof numberVar; type ovT = typeof objectVar; // T[key] type AP = A['a']; // string type BP = B[number]; // string type CN = C[number]; // 1 | 'number' | true type C0 = C[0]; // 1 // keyof T (T 会被立即求值) type AK = keyof A; // 惰性求值,其表达式类型等同于 "a"|"b"|"c" type BK = keyof C; // 惰性求值, 其表达式类型等同于 keyof Array let e: BK = 0;// correct e = 1; // correct e = 'toString';// correct e = 'includes';// correct e = 'a'; // wrong // 疑问:AK \ BK \ GK 出现惰性求值的现象,但是 FK 和 GK1 却出现了 立即求值。为什么会出现这种情况呢? typeof F 为什么会是 never,语意上如何解释? type FK = keyof F; // never type GK = keyof G; // 惰性求值(keyof A | keyof B),其表达式类型等同于 "a"|"b"|"c"|"d"|"e"|"f" type G1K = keyof G1; // "g" | "h" | "i" | "j" // A & B type AAB = A & B;// 惰性求值 let f:AAB = Object.assign({ a: '1', b: 1, c: true }, ['0']); type AAD = A & D;// 惰性求值 let g: AAD = { a: '1', b: 1, c: true, d: '2', e: 2, f: [true] } type HAI = H & I;// never // A | B type AOB = A | B; //惰性求值 let k: AOB = { a: '1', b: 1, c: true, }; k = ['1']; type BOC = B | C; //惰性求值 let l: BOC = ['1']; l = [1, "number", true]; type HOI = H | I; //惰性求值 // 疑问: 枚举的联合语法是上允许的, 语意上要怎么解释呢? 其类型值最终到底是什么? let m: HOI = H;// wrong m = I;// wrong m = { ...H, ...I } // wrong } // wrong // (第一部分)
展开