我最近将 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
错误消息适用于以下代码片段(带有行号):
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 ,但没有运气。有人知道如何解决吗?
最佳答案
这是因为 typescript 版本。
打开命令提示符或终端。然后运行这些命令。
检查 TypeScript 版本
tsc -v
应该高于2.4
如果没有。
全局安装最新版本的 typescript
npm install typescript -g
打开项目的 package.json 文件 并用新安装的版本像这样更改 typescript 版本
"typescript": "~2.6.1"
然后删除node_modules文件夹
使用清理缓存
npm cache clean
最后运行
npm install
*注意:您可以使用npm update更新npm,但不确定typescript版本是否会更新*
关于TypeScript 字符串枚举 - "Type ... is not assignable to type ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45191472/