TypeScript 的字符串枚举 - "Type ... is not assignable to type ..."
TypeScript's string enums - "Type ... is not assignable to type ..."
我最近将 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. }
我正在使用 Visual Studio 2017,它声称已安装 TypeScript 2.4.0:
我搜索了 TypeScript's issues,但没有成功。有人知道如何解决吗?
这是使用早于 2.4 的 typescript 版本编译时出现的错误。我只能建议您的 Visual Studio 副本以某种方式获取其自己的旧版本打字稿,而不是使用项目中安装的较新版本。有关更新打字稿的说明,请参阅维基 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>
灵感来自 , I found the root cause. Although the application was using TypeScript 2.4, VS's IntelliSense was still stuck in 2.3.
解决此问题的方法是下载并安装 TypeScript 2.4 SDK,然后从选项中 select 更新版本:
对我来说,问题是 @angular/cli
使用的是较低版本的 Typescript。检查您的锁定文件。它显示了 <2.4.0 的要求。例如,我们的项目使用 yarn.lock
。
编译的时候报了低版本Typescript相关的错误。为了解决这个问题,我在前面添加了兼容标志 ^
。所以对我们来说,它开始于:
"@angular/cli": "1.2.5"
...更改为:
"@angular/cli": "^1.2.5"
这似乎解决了这个问题。值得注意的是,它本质上是强制 cli
使用 Typescript 的工作区版本。对于我们来说,这是 2.4.0
,这个版本的 cli
在技术上不兼容(因为它需要 <2.4.0)。编译的时候有warning,不过暂时给我们成功了
这是因为 typescript 版本。
打开命令提示符或终端。然后 运行 这些命令。
检查 TypeScript 版本
tsc -v
应该高于2.4
如果没有。
全局安装最新版本的 typescript
npm install typescript -g
打开项目的 package.json 文件
并使用新安装的版本
像这样更改打字稿版本
"typescript": "~2.6.1"
然后删除node_modules文件夹
使用
清理缓存
npm cache clean
终于运行
npm install
*注意:可以使用 npm update 更新 npm 但不确定 typescript 版本是否会更新 *
我的 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 库。
在Visual Studio2017版本15.3及之后的版本中,使用的TypeScript版本绑定到各个项目。
参考 - https://github.com/Microsoft/TypeScript/wiki/Updating-TypeScript-in-Visual-Studio-2017
如果你和我一样,使用VS但不在项目中(只是打开一个文件夹),那么我只需要为VS安装最新版本的TS:
https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.typescript-27-vs2017
如果您使用的是 Visual studio 2017 社区版。你不会在 Tools/Options 中找到 TypeScript intellisense。
您应该编辑项目文件 .jsproj。
TypeScriptToolsVersion
并将 TypeScriptToolsVersion 更新为 2.6.2 或最新版本。
使用 'any' 进行类型转换会得到结果:
enum StepType {
ENGLISH = <any>'S',
}
我最近将 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. }
我正在使用 Visual Studio 2017,它声称已安装 TypeScript 2.4.0:
我搜索了 TypeScript's issues,但没有成功。有人知道如何解决吗?
这是使用早于 2.4 的 typescript 版本编译时出现的错误。我只能建议您的 Visual Studio 副本以某种方式获取其自己的旧版本打字稿,而不是使用项目中安装的较新版本。有关更新打字稿的说明,请参阅维基 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>
灵感来自
解决此问题的方法是下载并安装 TypeScript 2.4 SDK,然后从选项中 select 更新版本:
对我来说,问题是 @angular/cli
使用的是较低版本的 Typescript。检查您的锁定文件。它显示了 <2.4.0 的要求。例如,我们的项目使用 yarn.lock
。
编译的时候报了低版本Typescript相关的错误。为了解决这个问题,我在前面添加了兼容标志 ^
。所以对我们来说,它开始于:
"@angular/cli": "1.2.5"
...更改为:
"@angular/cli": "^1.2.5"
这似乎解决了这个问题。值得注意的是,它本质上是强制 cli
使用 Typescript 的工作区版本。对于我们来说,这是 2.4.0
,这个版本的 cli
在技术上不兼容(因为它需要 <2.4.0)。编译的时候有warning,不过暂时给我们成功了
这是因为 typescript 版本。
打开命令提示符或终端。然后 运行 这些命令。
检查 TypeScript 版本
tsc -v
应该高于2.4
如果没有。
全局安装最新版本的 typescript
npm install typescript -g
打开项目的 package.json 文件 并使用新安装的版本
像这样更改打字稿版本"typescript": "~2.6.1"
然后删除node_modules文件夹
使用
清理缓存npm cache clean
终于运行
npm install
*注意:可以使用 npm update 更新 npm 但不确定 typescript 版本是否会更新 *
我的 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 库。
在Visual Studio2017版本15.3及之后的版本中,使用的TypeScript版本绑定到各个项目。
参考 - https://github.com/Microsoft/TypeScript/wiki/Updating-TypeScript-in-Visual-Studio-2017
如果你和我一样,使用VS但不在项目中(只是打开一个文件夹),那么我只需要为VS安装最新版本的TS:
https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.typescript-27-vs2017
如果您使用的是 Visual studio 2017 社区版。你不会在 Tools/Options 中找到 TypeScript intellisense。 您应该编辑项目文件 .jsproj。 TypeScriptToolsVersion
并将 TypeScriptToolsVersion 更新为 2.6.2 或最新版本。
使用 'any' 进行类型转换会得到结果:
enum StepType {
ENGLISH = <any>'S',
}