(TS)在"const"枚举声明中,成员初始值设定项必须是常量表达式



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

(TS( 在"const"枚举声明中,成员初始值设定项必须是常量 表达。

构建:在"const"枚举声明中,成员初始值设定项必须是常量表达式。

有错误的代码:

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

我的包.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 但角度应用程序位于名为客户端应用程序的单独文件夹中。 我已经尝试了不同版本的 ts 和角度 cli,但没有任何帮助。我该怎么办 错误 ?

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

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

要拥有一个字符串枚举,你只需要执行以下操作

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

根据文档

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

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

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

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

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

@ben-Barreth 建议的答案对我来说似乎也是如此。VS Code 的 tsc 似乎是版本 <2。但是,与其升级 vscode 插件,更方便的方法是使用 npm 包,并显式路径到 tsc ./node_modules/.bin/tsc。

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

最新更新