gpt4 book ai didi

javascript - TypeScript:在 Enum 上执行 switch-case 来设置变量的惯用方法

转载 作者:行者123 更新时间:2023-12-02 18:03:28 29 4
gpt4 key购买 nike

示例问题:我有一个枚举变量难度。在函数中,我想根据 Difficulty 的值设置配置 DifficultyConfig 。这是我能想到的一种不优雅的方式:

export interface DifficultyConfig {
healthModifier: number,
deathIsPermanent: boolean,
}

export interface AppProps {
difficultyConfig: DifficultyConfig
}

export const NormalDifficultyConfig: DifficultyConfig = {
enemyHealthModifier: 1,
deathIsPermanent: false,
}

export const HigherDifficultyConfig: DifficultyConfig = {
enemyHealthModifier: 1.3,
deathIsPermanent: true,
}

export enum Difficulty {
NORMAL = 'Normal',
HARD = 'Hard',
ADVANCED = 'Advanced',
}

function createApp(difficulty: Difficulty) {
let difficultyConfig: DifficultyConfig;

switch(difficulty) {
case Difficulty.NORMAL:
difficultyConfig = NormalDifficultyConfig;
break;
// Both HARD and ADVANCED get HigherDifficultyConfig
case Difficulty.HARD:
difficultyConfig = HigherDifficultyConfig;
break;
case Difficulty.ADVANCED:
difficultyConfig = HigherDifficultyConfig;
break;
default:
difficultyConfig = NormalDifficultyConfig;
break;
}

return new App({
difficultyConfig
});
}

我不喜欢这么简单的 Switch case 语法。我的理想在 Scala 中是这样的:

val difficultyConfig = difficulty match {
case Difficulty.NORMAL => NormalDifficultyConfig
case Difficulty.HARD | Difficulty.ADVANCED => HigherDifficultyConfig
case _ => NormalDifficultyConfig
}

JavaScript 中有类似的东西吗?

最佳答案

如果逻辑存在差异,则 switchif/else if/else (稍微不太冗长)可能是可行的方法(下面有更多介绍)。如果它像您的示例中那样纯粹是数据,那么您可以使用配置难度映射对象:

const difficultyConfigs = {
[Difficulty.NORMAL]: NormalDifficultyConfig,
[Difficulty.HARD]: HigherDifficultyConfig,
[Difficulty.ADVANCED]: HigherDifficultyConfig,
} as const;

您甚至可以将其声明为 Record<Difficult, DifficultyConfig>像这样,如caTS在评论中指出:

const difficultyConfigs: Record<Difficult, DifficultyConfig> = {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[Difficulty.NORMAL]: NormalDifficultyConfig,
[Difficulty.HARD]: HigherDifficultyConfig,
[Difficulty.ADVANCED]: HigherDifficultyConfig,
} as const;

稍后会详细介绍,但无论哪种方式,该函数都只是:

function createApp(difficulty: Difficulty) {
let difficultyConfig = difficultyConfigs[difficulty];

return new App({
difficultyConfig
});
}

Playground 链接:Without Record | With Record (我还更新了问题代码中的一些看似拼写错误/编辑错误。)

这还有一个优点,如果您添加 Difficulty但忘记在 difficultyConfigs 中包含一个,当您使用它时,您会遇到一个方便的编译错误。我模拟过这样的错误here通过添加 MEDIUM困难却“忘记”更新功能。

但更好的是,如果我们包含类型 Record<Difficulty, DifficultyConfig>输入 caTS建议和difficultyConfigs没有每个 Difficulty 的条目值,您会得到更早的编译时错误,like this .


即使对于逻辑,如果您愿意,也可以使用相同的概念来创建调度对象:

const difficultyConfigs: Record<Difficulty, () => DifficultyConfig> = {
[Difficulty.NORMAL]: () => { /*...build and return NORMAL config...*/ },
[Difficulty.HARD]: () => { /*...build and return HARD config...*/ },,
[Difficulty.ADVANCED]: () => { /*...build and return ADVANCED config...*/ },,
} as const;
// ...
const difficultyConfig = difficultyConfigs[difficulty]();

关于javascript - TypeScript:在 Enum 上执行 switch-case 来设置变量的惯用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73800379/

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