interface Test {
test: string
name: string
str: string
num: number
}
type OneOf<T> = ...;
const test: OneOf<Test> = { num: 123 }; // 正确
const test: OneOf<Test> = { test: 'asdasd' }; // 正确
const test1: OneOf<Test> = { test: 'asdasd', name: 'namessss' }; // 错误
const test2: OneOf<Test> = { test: 2123 }; // 错误
const test3: OneOf<Test> = {}; // 错误
其实就是定义一个类型,这个类型指定的是某个对象类型其中一个键和值,只能是一个,不能为空,不能为多个,也不能值类型不对。
如何定义OneOf
,interface也行