gpt4 book ai didi

javascript - 是否有与 TypeScript 中的 Spread Operator 等效的用于接口(interface)的功能?

转载 作者:行者123 更新时间:2023-12-02 16:38:58 25 4
gpt4 key购买 nike

我正在从事一个 JavaScript 项目,并且也想使用 TypeScript。在一种情况下,如果我可以减少此界面中的重复次数,那就太好了:-

interface Question {
settings: {
label: string
description: string
hidden: boolean
followUp: {
label: string
description: string
hidden: boolean
}
}
}

如果有某种传播运算符会很棒:

interface QuestionSettings {
label: string
description: string
hidden: boolean
}

interface Question {
settings: {
...QuestionSettings
followUp: QuestionSettings
}
}

这在 TypeScript 中可能吗?

最佳答案

你可以使用 intersection types :

interface QuestionSettings {
label: string;
description: string;
hidden: boolean;
}

interface Question {
settings: QuestionSettings & {
followUp: QuestionSettings;
}
}

关于javascript - 是否有与 TypeScript 中的 Spread Operator 等效的用于接口(interface)的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62061796/

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