【发布时间】:2022-06-11 03:20:21
【问题描述】:
在下面的例子中:
type TA = { a: 1 }
type TB = { b: 2 }
const testa: TA = {
a: 1
}
const testb: TB = {
b: 2
}
我只想允许类型为 TA 或类型为 TB 的对象,而不是组合对象。 TypeScript 中允许以下内容:
const testEitherOr: TA | TB = {
a: 1,
b: 2 // This seems like it should not be allowed
}
如何确保test 仅匹配两种对象类型之一?
【问题讨论】:
标签: typescript