题目:让 a、b、c 三种赋值同时成立(联合类型)
给你 test 对象,利用它生成一个类型 Test,要求 a、b、c 同时成立。
const test = { SUCCESS: 100, FAIL: 500 }
// a, b, c 同时成立
const a: Test = { FAIL: 500 }
const b: Test = { SUCCESS: 100 }
const c: Test = { SUCCESS: 100, FAIL: 500 }
export type Test = ???
答案
export type Test = {
[K in keyof typeof test]: { [P in K]: (typeof test)[K] }
}[keyof typeof test]
来源整理自:vue3js.cn 面试官系列



