TypeScript的字符串枚举 - "类型...不能分配到类型..."

37

我最近将TypeScript版本从2.3.4升级到2.4.0,希望能够使用字符串枚举。然而,令我失望的是,我遇到了以下错误信息:

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的问题中进行了搜索,但没有结果。 有人知道如何解决吗?


错误来源在 process.model.ts 文件中。你能分享一下吗? - rzelek
错误明显与代码片段相关联。一旦您删除赋值(或使用数字替代),错误就会消失。有些事情告诉我,这是TypeScript实现中的一个bug。 - Dawid O
1
错误提示为“类型 E 无法分配给 StepType”。您提供的代码片段是声明,但错误明确指出了赋值。在下一行中,您可以看到有关文件和行号的信息(process.model.ts 第17行)。 - rzelek
@ArekŻelechowski 请阅读此消息。它抱怨类型而非值的赋值。当您在 TypeScript 版本低于 2.4 的代码片段中使用此代码时,确实会收到此消息。我怀疑即使项目安装了更新版本,Visual Studio 仍然能够捕获旧版本。 - Duncan
@Duncan 感谢您的清晰解释。很高兴了解到这个问题,看起来错误相当神秘。此外,在我发表评论时,没有关于行号的信息,我认为 process.model.ts 文件中并没有包含 StepType 定义,而是使用该枚举的代码。 - rzelek
只有在删除了我的VS并安装了最新版本的它(在我的情况下是VS Community 2017,v.15.5.2)之后,我才解决了这个问题。即使只是更新VS也没有帮助。对于这种行为没有任何想法。 - Ilya Loskutov
9个回答

30

这是因为 TypeScript 的版本。

打开命令提示符或终端,然后运行以下命令。

检查 TypeScript 版本

tsc -v

应该大于2.4。

如果不是,则需要全局安装最新版本的TypeScript。

安装最新版本的TypeScript全局

npm install typescript -g

打开项目中的 package.json 文件,并根据新安装的版本更改 typescript 版本,如下所示

"typescript": "~2.6.1"

然后删除 node_modules 文件夹

清除缓存使用

npm cache clean

最后运行

npm install

*请注意:您可以使用npm update更新npm,但无法保证typescript版本会被更新*


3
目前我在一个版本为1.7.3的Angular CLI项目中运行tsc 2.8.3,但我仍然遇到这个错误。 - Xerri
3
我也是,我使用更高版本的typescript,但仍然遇到了错误。 - Murhaf Sousli

8
这是在使用2.4版本以下的TypeScript编译时出现的错误。我能建议的是,你的Visual Studio复制品可能会自动选择旧版的TypeScript而不是使用项目中安装的新版TypeScript。请参考维基https://github.com/Microsoft/TypeScript/wiki/Updating-TypeScript-in-Visual-Studio-2017上的说明来更新TypeScript。
PS C:\temp> cat t.ts
enum StepType {
    Start = 'S',
    Activity = 'A',
    Decision = 'D',
    End = 'E'
}
PS C:\temp> node somepath\node_modules\typescript\bin\tsc --version
Version 2.2.2
PS C:\temp> node somepath\node_modules\typescript\bin\tsc t.ts
t.ts(2,13): error TS2322: Type '"S"' is not assignable to type 'StepType'.
t.ts(3,16): error TS2322: Type '"A"' is not assignable to type 'StepType'.
t.ts(4,16): error TS2322: Type '"D"' is not assignable to type 'StepType'.
t.ts(5,11): error TS2322: Type '"E"' is not assignable to type 'StepType'.
PS C:\temp> tsc --version
Version 2.4.1
PS C:\temp> tsc t.ts
PS C:\temp>

5
Duncan的回答启发,我找到了根本原因。尽管应用程序使用的是TypeScript 2.4,但VS的IntelliSense仍然停留在2.3上。VS IntelliSense未更新 解决问题的方法是下载并安装TypeScript 2.4 SDK,然后从选项中选择更新版本。

enter image description here


这并没有给我在设置中更改的选项,但是Typescript 2.4 SDK确实解决了我的问题。谢谢。 - Tracy Moody
在WebStorm中我也遇到了同样的问题... TypeScript版本已在设置中被固定为2.2。当我选择自动检测时,它可以工作! - Stefan

1

使用“任意”进行类型转换将起作用:

enum StepType {
    ENGLISH = <any>'S',
}

1
对我来说,问题在于@angular/cli使用了一个较低版本的Typescript。查看你的锁定文件,例如我们的项目使用yarn.lock。编译时,它会抛出与较低版本Typescript相关的错误。为了解决这个问题,我在前面添加了兼容标志^。因此,对我们而言,它起始于: "@angular/cli": "1.2.5" 改为: "@angular/cli": "^1.2.5" 这似乎解决了问题。值得注意的是,它实际上强制cli使用工作区版本的Typescript。对我们来说,这是2.4.0,但这个版本的cli技术上不兼容(因为它需要<2.4.0)。编译时会抛出一个警告,但对我们来说已经成功地工作了一段时间。

0

0
如果您正在使用Visual Studio 2017社区版,您将无法在“工具/选项”中找到TypeScript智能感知。您应该编辑项目文件.jsproj。 TypeScriptToolsVersion 并将TypeScriptToolsVersion更新为2.6.2或最新版本。

0

0

我在我的Angular2项目中遇到了同样的问题。我需要更新Angular2项目中的TypeScript(TS)库。

1)在您的package.json文件中,在“devDependencies”部分中添加以下内容:

"ts-node": "~3.2.0",
"tslint": "~5.7.0",
"typescript": "~2.4.2"

所以我的看起来像:

  "devDependencies": {
    "@angular/compiler-cli": "^2.3.1",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.42",
    "angular-cli": "1.0.0-beta.28.3",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "~4.0.13",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.4.2"
  }

2) 从您的项目中删除 "node_modules" 包和 "package-lock.json" 文件。

3) 在命令行上执行 "npm install" 以安装所有新的 TS 库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接