【问题标题】:TypeScript's string enums - "Type ... is not assignable to type ..."TypeScript 的字符串枚举 - “Type ... 不可分配给 type ...”
【发布时间】:2017-12-24 18:41:55
【问题描述】:

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

错误消息适用于以下代码 sn-p(带有行号):

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,但没有运气。有人知道怎么解决吗?

【问题讨论】:

  • 错误来源在 process.model.ts 文件中。可以分享一下吗?
  • 错误肯定与代码sn-p有关。一旦您删除分配(或改用数字),错误就会消失。有人告诉我,这是 TypeScript 实现中的一个错误
  • 错误状态“E 型不可分配给 StepType”。您提供的 sn-p 是声明,但错误清楚地说明了分配。下一行你有关于文件和行的信息(process.model.ts 第 17 行)。
  • @ArekŻelechowski 阅读了该消息。它抱怨类型而不是值的分配。这确实是您在 2.4 之前的 typescript 版本上使用 sn-p 中的代码时收到的消息。我怀疑即使项目安装了较新的版本,Visual Studio 仍在设法选择较旧的版本。
  • @Duncan 感谢您的精彩解释。很高兴知道未来,错误似乎很神秘。此外,在我发表评论时,没有关于行号的信息,我认为 process.model.ts 不包含 StepType 定义,而是使用此枚举的代码。

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


【解决方案1】:

这是因为打字稿版本。

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

检查 TypeScript 版本

tsc -v

应该高于 2.4

如果没有。

在全球范围内安装最新版本的打字稿

npm install typescript -g

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

"typescript": "~2.6.1"

然后删除 node_modules 文件夹

使用清理缓存

npm cache clean

终于运行了

npm install

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

【讨论】:

  • 当前在 v1.7.3 的 Angular cli 项目中运行 tsc 2.8.3 但我仍然收到错误。
  • 我也是,我有更大的打字稿版本,但仍然收到错误
【解决方案2】:

这是您在使用早于 2.4 的 typescript 版本进行编译时遇到的错误。我只能建议您的 Visual Studio 副本以某种方式获取其自己的旧版本的打字稿,而不是使用项目中安装的较新版本。有关更新 typescript 的说明,请参阅 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>

【讨论】:

    【解决方案3】:

    受到Duncan's answer的启发,我找到了根本原因。尽管该应用程序使用的是 TypeScript 2.4,但 VS 的 IntelliSense 仍停留在 2.3 中。

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

    【讨论】:

    • 这并没有让我选择在设置中更改它,但是 Typescript 2.4 SDK 确实解决了我的问题。谢谢。
    • 在 WebStorm 中对我来说也是如此...TypeScript 版本在设置中固定为 2.2。当我选择自动检测时,它起作用了!
    【解决方案4】:

    对我来说,问题在于 @angular/cli 使用的是较低版本的 Typescript。查看您的锁定文件。它显示了 yarn.lock。

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

    "@angular/cli": "1.2.5"

    ...改为:

    "@angular/cli": "^1.2.5"

    这似乎解决了这个问题。值得注意的是,它本质上是强制 cli 使用 Typescript 的工作区版本。对我们来说,这是2.4.0,这个版本的cli 在技术上不兼容(因为它需要

    【讨论】:

      【解决方案5】:

      使用 'any' 进行类型转换会成功:

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

      【讨论】:

        【解决方案6】:

        我的 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 库。

        【讨论】:

          【解决方案7】:

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

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

          【讨论】:

            【解决方案8】:

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

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

            【讨论】:

              【解决方案9】:

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

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

              【讨论】:

                猜你喜欢
                • 2021-10-01
                • 1970-01-01
                • 2018-03-20
                • 1970-01-01
                • 1970-01-01
                • 2022-08-04
                • 1970-01-01
                • 1970-01-01
                • 2019-06-27
                相关资源
                最近更新 更多