TypeScript 的字符串枚举 - “类型 ... 不可分配给类型 ...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45191472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:46:03  来源:igfitidea点击:

TypeScript's string enums - "Type ... is not assignable to type ..."

typescriptenumsvisual-studio-2017typescript2.4

提问by Dawid O

I have recently upgraded the version of TypeScript from 2.3.4 to 2.4.0 hoping to use the string enums. To my dismay, however, I have been greeted with the error messages:

我最近将 TypeScript 的版本从 2.3.4 升级到 2.4.0,希望能使用字符串 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
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

The error messages apply to the following code snippet (with the line numbers):

错误消息适用于以下代码片段(带有行号):

13. export enum StepType {
14.    Start = 'S',
15.    Activity = 'A',
16.    Decision = 'D',
17.    End = 'E'
18. }

I am using Visual Studio 2017 which claims TypeScript 2.4.0 is installed:

我正在使用声称安装了 TypeScript 2.4.0 的 Visual Studio 2017:

enter image description here

在此处输入图片说明

I searched through TypeScript's issues, but without luck. Does anybody know how to fix it?

我搜索了TypeScript 的问题,但没有运气。有人知道如何解决吗?

采纳答案by Dawid O

Inspired by Duncan's answer, I found the root cause. Although the application was using TypeScript 2.4, VS's IntelliSense was still stuck in 2.3. VS IntelliSense was not updated

受到邓肯回答的启发,我找到了根本原因。尽管该应用程序使用的是 TypeScript 2.4,但 VS 的 IntelliSense 仍然停留在 2.3 中。VS IntelliSense 未更新

The way to resolve the issue was to download and install TypeScript 2.4 SDKand then select from the options the newer version:

解决问题的方法是下载并安装TypeScript 2.4 SDK,然后从选项中选择较新的版本:

enter image description here

在此处输入图片说明

回答by Chanaka Fernando

This is because typescript version.

这是因为打字稿版本。

Open command prompt or terminal. then run these commands.

打开命令提示符或终端。然后运行这些命令。

Check TypeScript version

检查 TypeScript 版本

tsc -v

should be higher than 2.4

应该高于 2.4

if not.

如果不。

install latest version of typescript globally

全局安装最新版本的打字稿

npm install typescript -g

Open your package.json file of the project and change typescript version like this with newly installed version

打开项目的 package.json 文件并使用新安装的版本更改打字稿版本

"typescript": "~2.6.1"

Then delete node_modules folder

然后删除 node_modules 文件夹

Clean cache using

使用清理缓存

npm cache clean

Finally run

最后运行

npm install

*Note that: You can update npm using npm updatebut it is not sure that the typescript version will be updated *

*注意:您可以使用npm update更新 npm,但不确定打字稿版本是否会更新*

回答by Duncan

This is the error you get when compiling with a version of typescript older than 2.4. All I can suggest is that your copy of Visual Studio is somehow picking up its own older version of typescript rather than using the newer one installed in your project. See the wiki https://github.com/Microsoft/TypeScript/wiki/Updating-TypeScript-in-Visual-Studio-2017for instructions on updating typescript.

这是使用早于 2.4 的打字稿版本编译时遇到的错误。我只能建议您的 Visual Studio 副本以某种方式获取自己的旧版本打字稿,而不是使用项目中安装的较新版本。有关更新打字稿的说明,请参阅 wiki https://github.com/Microsoft/TypeScript/wiki/Updating-TypeScript-in-Visual-Studio-2017

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>

回答by kbpontius

For me, the problem was that @angular/cliwas using a lower version of Typescript. Check out your lock file. It was showing a requirement of <2.4.0. Our project uses yarn.lock, for example.

对我来说,问题在于@angular/cli使用了较低版本的 Typescript。检查您的锁定文件。它显示了 <2.4.0 的要求。yarn.lock例如,我们的项目使用。

When it compiled, it was throwing an error related to the lower version of Typescript. To fix the problem, I added the compatible flag ^to the front. So for us, it started as:

当它编译时,它抛出了一个与较低版本的 Typescript 相关的错误。为了解决这个问题,我^在前面添加了兼容标志。所以对我们来说,它开始于:

"@angular/cli": "1.2.5"

"@angular/cli": "1.2.5"

...changed to:

...变成:

"@angular/cli": "^1.2.5"

"@angular/cli": "^1.2.5"

This seems to fix the issue. It's worth noting that it essentially forces clito use the workspace version of Typescript. For us, this is 2.4.0, which this version of cliisn't technically compatible with (since it requires <2.4.0). It throws a warning when compiling, but it has worked successfully for us for the time being.

这似乎解决了这个问题。值得注意的是,它本质上是强制cli使用 Typescript 的工作区版本。对我们来说,这是2.4.0,这个版本在cli技术上不兼容(因为它需要 <2.4.0)。它在编译时抛出一个警告,但它暂时对我们来说已经成功了。

回答by Gene

I had the same issues for my Angular2 project. I needed to update the Typescript (TS) library in my Angular2 project.

我的 Angular2 项目也有同样的问题。我需要更新我的 Angular2 项目中的 Typescript (TS) 库。

1) Inside your package.json, add this to the "devDependencies" section:

1) 在 package.json 中,将其添加到“devDependencies”部分:

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

So mine looks like:

所以我的看起来像:

  "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) Delete "node_modules" package and "package-lock.json" file from your project.

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

3) Do "npm install" on your command line in order to install all the new TS libraries.

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

回答by ppenchev

In Visual Studio 2017 version 15.3 and later, the TypeScript version used is bound to individual projects.

在 Visual Studio 2017 版本 15.3 及更高版本中,使用的 TypeScript 版本绑定到单个项目。

Reference - https://github.com/Microsoft/TypeScript/wiki/Updating-TypeScript-in-Visual-Studio-2017

参考 - https://github.com/Microsoft/TypeScript/wiki/Updating-TypeScript-in-Visual-Studio-2017

回答by DoubleA

If you are like me, using VS but not in project (just opening a folder), then I just had to install the latest version of TS for VS:

如果你像我一样,使用 VS 但不在项目中(只是打开一个文件夹),那么我只需要为 VS 安装最新版本的 TS:

https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.typescript-27-vs2017

https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.typescript-27-vs2017

回答by Zeeshan Anjum

If you are using Visual studio 2017 Community version. you will not find TypeScript intellisense in Tools/Options. You should edit the project file .jsproj. TypeScriptToolsVersion

如果您使用的是 Visual Studio 2017 社区版。你不会在工具/选项中找到 TypeScript 智能感知。您应该编辑项目文件 .jsproj。 打字稿工具版本

and update TypeScriptToolsVersion to 2.6.2 or latest version.

并将 TypeScriptToolsVersion 更新到 2.6.2 或最新版本。