gpt4 book ai didi

javascript - typescript 接口(interface) : Exactly one optional parameter is required

转载 作者:搜寻专家 更新时间:2023-10-30 20:59:47 24 4
gpt4 key购买 nike

我想定义一个接口(interface),允许您提供 contentcontent_object 但不能同时提供两者。你必须定义一个或另一个。在 TypeScript 中实现这一点的最简单方法是什么?我知道我可以说内容是 string | object,但如果我可以按照描述的方式定义它,我的其余代码就会受益。

interface IModal {
content?: string;
content_object?: object;
}

最佳答案

type IModal = { content: string; content_object?: undefined } |
{ content_object: object; content?: undefined }

这个答案只包含代码,因此根据自动化系统是错误的。

关于javascript - typescript 接口(interface) : Exactly one optional parameter is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50282183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com