【发布时间】:2017-12-24 18:41:55
【问题描述】:
我最近将 TypeScript 的版本从 2.3.4 升级到了 2.4.0,希望能使用 string enums。然而,令我沮丧的是,我收到了错误消息:
Severity Code Description Project File Line Suppression State Error TS2322 Type '"E"' is not assignable to type 'StepType'. ClientApp (tsconfig project) Z:\SMART\Smart\Smart\ClientApp\app\models\process.model.ts 17 Active Error TS2322 Type '"S"' is not assignable to type 'StepType'. ClientApp (tsconfig project) Z:\SMART\Smart\Smart\ClientApp\app\models\process.model.ts 14 Active Error TS2322 Type '"A"' is not assignable to type 'StepType'. ClientApp (tsconfig project) Z:\SMART\Smart\Smart\ClientApp\app\models\process.model.ts 15 Active Error TS2322 Type '"D"' is not assignable to type 'StepType'. ClientApp (tsconfig project) Z:\SMART\Smart\Smart\ClientApp\app\models\process.model.ts 16 Active
错误消息适用于以下代码 sn-p(带有行号):
13. export enum StepType {
14. Start = 'S',
15. Activity = 'A',
16. Decision = 'D',
17. End = 'E'
18. }
我正在使用声称已安装 TypeScript 2.4.0 的 Visual Studio 2017:
我搜索了TypeScript's issues,但没有运气。有人知道怎么解决吗?
【问题讨论】:
-
错误来源在 process.model.ts 文件中。可以分享一下吗?
-
错误肯定与代码sn-p有关。一旦您删除分配(或改用数字),错误就会消失。有人告诉我,这是 TypeScript 实现中的一个错误
-
错误状态“E 型不可分配给 StepType”。您提供的 sn-p 是声明,但错误清楚地说明了分配。下一行你有关于文件和行的信息(process.model.ts 第 17 行)。
-
@ArekŻelechowski 阅读了该消息。它抱怨类型而不是值的分配。这确实是您在 2.4 之前的 typescript 版本上使用 sn-p 中的代码时收到的消息。我怀疑即使项目安装了较新的版本,Visual Studio 仍在设法选择较旧的版本。
-
@Duncan 感谢您的精彩解释。很高兴知道未来,错误似乎很神秘。此外,在我发表评论时,没有关于行号的信息,我认为
process.model.ts不包含StepType定义,而是使用此枚举的代码。
标签: typescript enums visual-studio-2017 typescript2.4