我遇到了有关定义类型和检查值是否包含在该类型中的问题。
这是我的示例:
以下是各种类型:
export type Key = 'features' | 'special';
export type TabTypes = 'info' | 'features' | 'special' | 'stars';
当用户更改选项卡时,它会发送一个字符串值,其类型为TabTypes。
activeTabChanged(event: TabTypes) {
this.activeTab: TabTypes = event;
// it won't let me set the key here because key has a different type
// but the send event can be contained in type Key
// how can I check if the send event from type TabTypes is contained in type Key
this.key: Key = event;
}
有没有一种TypeScript的方法可以检查一个具有类型的发送值是否可以等于来自不同类型的值?
currentKey in Keys
只适用于数字枚举,无法用于字符串枚举。 - Maor RefaelicurrentKey in Keys
根本不起作用。这个问题为什么会有赞成票很奇怪,因为如果你尝试这样做,它是行不通的。我花了好15分钟重构我的代码,结果当我运行代码时它却不起作用。 - KG23