简体   繁体   English

Typescript,如何摆脱这些switch语句

[英]Typescript, how to get rid of these switch statements

Typescript basic question; Typescript 基本问题;

loadedExercise.exercise contains an exercise that has been loaded from an API. loadedExercise.exercise包含一个从 API 加载的练习。 After the load, I apply some computations to that loaded exercise and I leave them inside translationsAudiosOthers .加载后,我对加载的练习应用一些计算,并将它们留在translationsAudiosOthers中。 Then I create a number of fields containing statistics that are different for each type of exercise.然后,我创建了许多字段,其中包含针对每种锻炼类型的不同统计信息。 visitedBranch is a formal parameter that a function containing the mentioned switch statement receives, which indicates the specific statistic field of that exercise that I want to retrieve so I can show it in a react UI visitedBranch是包含上述 switch 语句的 function 接收的形式参数,它指示我要检索的该练习的特定统计字段,以便我可以在反应 UI 中显示它

Given a switch/case like this, how would you improve it?给定这样的开关/外壳,您将如何改进它? I don't want to maintain it in case I add more exercise types and I'm 100% sure there must be a better way of doing this using generics or similar我不想维护它以防我添加更多运动类型,并且我 100% 确定必须有更好的方法来使用 generics 或类似方法

switch (loadedExercise.exercise.type) {
      case ExerciseTypes.typeA:
        return (loadedExercise.exercise.translationsAudiosOthers as TypeAExerciseLocalizations)[
          visitedBranch as TypeAStatBranches
        ];

      case ExerciseTypes.typeB:
        return (loadedExercise.exercise
          .translationsAudiosOthers as TypeBExerciseLocalizations)[
          visitedBranch as TypeBStatBranches
        ];

      case ExerciseTypes.TypeN:
        return (loadedExercise.exercise
          .translationsAudiosOthers as TypeNExerciseLocalizations)[
          visitedBranch as TypeNStatBranches
        ];
    }

Please note I am trying to avoid using any as in请注意,我试图避免使用any

return (loadedExercise.exercise.translationsAudiosOthers as any)[
          visitedBranch as any
        ];

as I lose autocomplete and I know we must avoid using any everytime we can.因为我失去了自动完成功能,我知道我们必须尽可能避免使用any功能。

A simple workaround is to create a interface to map out the types you want(assuming ExerciseTypes is a enum):一个简单的解决方法是创建一个到 map 的接口出你想要的类型(假设 ExerciseTypes 是一个枚举):

interface ExerciseTypeToLocalizations {
    [ExerciseTypes.typeA]: TypeAExerciseLocalizations;
    [ExerciseTypes.typeB]: TypeBExerciseLocalizations;
    [ExerciseTypes.typeN]: TypeNExerciseLocalizations;
}
interface ExerciseTypeToStatBranches {
    [ExerciseTypes.typeA]: TypeAStatBranches;
    [ExerciseTypes.typeB]: TypeBStatBranches;
    [ExerciseTypes.typeN]: TypeNStatBranches;
}

// instead of a switch statement
return (
    loadedExercise.exercise.translationsAudiosOthers 
    as ExerciseTypeToLocalizations[typeof loadedExercise.exercise.type]
)[visitedBranch as ExerciseTypeToStatBranches[typeof loadedExercise.exercise.type]]

With what was presented, this is the best I can do.有了所呈现的,这是我能做的最好的。 If you can provide more context to the code I'm sure there is a more elegant way using generics.如果您可以为代码提供更多上下文,我相信使用 generics 会有更优雅的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM