TypeScript 字符串枚举 - "Type ... is not assignable to type ..."

标签 typescript enums visual-studio-2017 typescript2.4

我最近将 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:

enter image description here

我搜索了 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/

相关文章:

visual-studio-2017 - Visual Studio 构建工具 2017 离线安装程序

javascript - 无法从 Angular 9 中的服务加载变量

angular - 无法成功编译我的 Angular 应用程序

java - 要存储在数据库中的枚举

c# - .NET Core 2.0 中 PropertyInfo.IsPublic 的等价物

c++ - 在 Visual Studio 2017 上无法使用 x64 Debug模式进行 opencv3.2 编译

google-chrome - 尝试使用 Debugger For Chrome 插件在 VSCode 中使用 "Attach to Chrome"选项进行调试时出错

javascript - 如何将用户ID从浏览器传递到 Protractor ?

java - 在多应用程序环境中处理枚举

c# - 对 DropdownList 使用枚举 : Missing a using directive or assembly reference