相关疑难解决方法(0)

打字稿界面 - 可以使"一个或另一个"属性成为必需吗?

可能是一个奇怪的问题,但我很好奇是否可以创建一个需要一个属性或另一个属性的接口.

所以,例如......

interface Message {
    text: string;
    attachment: Attachment;
    timestamp?: number;
    // ...etc
}

interface Attachment {...}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我想确定是存在text还是attachment存在.

希望这是有道理的.

提前致谢!


编辑:这就是我现在正在做的事情.认为它有点冗长(输入botkit for slack).

interface Message {
    type?: string;
    channel?: string;
    user?: string;
    text?: string;
    attachments?: Slack.Attachment[];
    ts?: string;
    team?: string;
    event?: string;
    match?: [string, {index: number}, {input: string}];
}

interface AttachmentMessageNoContext extends Message {
    channel: string;
    attachments: Slack.Attachment[];
}

interface TextMessageNoContext extends Message {
    channel: string;
    text: string;
}
Run Code Online (Sandbox Code Playgroud)

typescript

34
推荐指数
9
解决办法
2万
查看次数

标签 统计

typescript ×1