【问题标题】:(TS) In 'const' enum declartions member initializer must be constant expression(TS) 在 'const' 枚举声明中,成员初始值设定项必须是常量表达式
【发布时间】:2018-05-27 16:04:59
【问题描述】:

我在 Visual Studio 2017 中构建应用程序时遇到问题。我正在使用 ASP.NET CORE 2 和 Angular 6。运行应用程序后,我在 node_modules 的文件 output_ast.d.ts 中遇到错误:

(TS) 在 'const' 枚举声明中,成员初始化器必须是常量 表达。

构建:在 'const' 枚举声明中,成员初始化程序必须是常量表达式。

错误代码:

export declare const enum JSDocTagName {
    Desc = "desc",
    Id = "id",
    Meaning = "meaning",
}

我的 package.json

  "name": "client-app",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.0",
    "@angular/common": "^6.0.0",
    "@angular/compiler": "^6.0.0",
    "@angular/core": "^6.0.0",
    "@angular/forms": "^6.0.0",
    "@angular/http": "^6.0.0",
    "@angular/platform-browser": "^6.0.0",
    "@angular/platform-browser-dynamic": "^6.0.0",
    "@angular/router": "^6.0.0",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^6.0.0",
    "@angular-devkit/build-angular": "~0.6.1",
    "typescript": "~2.7.2",
    "@angular/cli": "~6.0.1",
    "@angular/language-service": "^6.0.0",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~1.4.2",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  }
}

申请类似于本文https://www.codeproject.com/Articles/1245243/How-to-Create-an-App-with-Angular-and-ASP-NET-C 但角度应用程序位于名为 ClientApp 的单独文件夹中。 我尝试了不同版本的 ts 和 angular cli,但没有任何帮助。我能用这个错误做什么?

【问题讨论】:

    标签: asp.net angular typescript


    【解决方案1】:

    在 Visual Studio 中,检查项目“属性”->“TypeScript 构建”部分中的设置,并确保“TypeScript 版本”设置为“使用最新可用”或至少设置为最近的版本。我怀疑它当前设置为 TypeScript 支持字符串枚举之前的设置。

    【讨论】:

      【解决方案2】:

      我遇到了完全相同的行为,并且似乎是 TS 版本不兼容问题。我没有像上面建议的那样将 TypeScript 版本更改为“使用最新可用”,而是通过将 TypeScript 版本更改为 2.2 来解决。

      【讨论】:

        【解决方案3】:

        要获得字符串枚举,您只需执行以下操作

        export enum JSDocTagName {
            Desc = "desc",
            Id = "id",
            Meaning = "meaning",
        }
        

        根据documentation

        【讨论】:

        • 还是同样的错误。也许我问错了问题。我应该怎么做才能构建这个应用程序?
        • 如果您遵循标准,ng buildnpm run build 应该可以工作。没有你的 package.json 的内容,不知道你是如何创建你的应用程序的,我帮不了你。
        • 我已将 package.json 添加到主帖并描述了我如何创建我的应用程序。你能看一下吗?
        【解决方案4】:

        tldr;升级 Visual Studio 正在使用的 TypeScript 编译器。

        Visual Studio 2017 可能正在尝试使用比 2.4 更旧版本的 TypeScript 构建项目,当时引入了枚举的字符串初始值设定项:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4.html

        这里有 3 种不同的 TypeScript 编译器需要注意:

        • Angular CLI:使用本地项目 node_modules/typescript 文件夹
        • NPM:使用全局安装的 TS 编译器(Windows 上的 %AppData%/npm/Roaming)
        • Visual Studio:在 C:\Program Files (x86)\Microsoft SDKs\TypeScript 中使用 TS 编译器

        要升级 VS 使用的 TypeScript 编译器,请转到 https://marketplace.visualstudio.com 并搜索最新的 TypeScript 扩展。安装它,重新启动 Visual Studio,然后在项目设置中确保 TypeScript 构建版本设置为该新版本。现在,如果您重建,错误应该会消失。

        【讨论】:

          【解决方案5】:

          @ben-barreth 建议的答案对我来说似乎也是如此。 VS Code 的 tsc 似乎是版本

          $ ./node_modules/.bin/tsc --version
          Version 4.3.2
          
          $ tsc --version
          message TS6029: Version 1.5.3
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-08-24
            • 1970-01-01
            • 2014-06-30
            • 1970-01-01
            • 1970-01-01
            • 2019-06-12
            相关资源
            最近更新 更多