与XOR,{bar:string} XOR {can:numbers}的打字稿接口
我怎么说我想让一个接口是一个或另一个,而不是两者兼而有之?
interface IFoo {
bar: string /*^XOR^*/ can: number;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以与
从不
从不 /a>实现这一目标的类型:You can use union types along with the
never
type to achieve this:如这个问题,您可以使用
像这样:
As proposed in this issue, you can use conditional types (introduced in Typescript 2.8) to write a XOR type:
And you can use it like so:
您可以使用Union和可选的 noreferrer“> void Type:
但是,您必须使用
- strictnullchecks
以防止都没有。You can get "one but not the other" with union and optional void type:
However, you have to use
--strictNullChecks
to prevent having neither.尝试以下操作:
try this: