gpt4 book ai didi

javascript - JS 流 : What is the most clean and concise way to define an enum that is also treated like a type

转载 作者:行者123 更新时间:2023-11-30 14:45:02 24 4
gpt4 key购买 nike

来自强类型语言,我最近迈出了 React Native 的第一步。我使用 Flow 作为静态类型检查器。但是,我还没有找到一种简洁明了的方法来定义像类型一样处理的枚举类型,并触发“编译器”向我建议 VS Code 中的可能值。

目前我尝试通过以下方式实现这一点

export const CompletionStates = {
CompletionStateUnbegun: 'unbegun',
CompletionStateRunning: 'running',
CompletionStateLost: 'lost',
CompletionStateWon: 'won',
CompletionStateCancelled: 'cancelled',
}

export type CompletionState =
| CompletionStates.CompletionStateUnbegun
| CompletionStates.CompletionStateRunning
| CompletionStates.CompletionStateLost
| CompletionStates.CompletionStateWon
| CompletionStates.CompletionStateCancelled;

有谁知道使用 Flow 定义枚举的更简洁明了的方法。

最佳答案

您可以使用 $Values:

type CompletionStates = {
CompletionStateUnbegun: 'unbegun',
CompletionStateRunning: 'running',
CompletionStateLost: 'lost',
CompletionStateWon: 'won',
CompletionStateCancelled: 'cancelled',
};

export type CompletionState = $Values<CompletionStates>;

关于javascript - JS 流 : What is the most clean and concise way to define an enum that is also treated like a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49067859/

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