網站搜尋

asp.net - (TS) 在 'const' 列舉宣告中,成員初始化器必須是常數表達式


我在 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,但 Angular 應用程式放在名為 ClientApp 的獨立資料夾中。我已嘗試不同版本的 ts 和 Angular CLI,但都無濟於事。針對這個錯誤,我該怎麼做呢?

答案#1

在 Visual Studio 中,檢查您專案的屬性 -> TypeScript 建置部分的設定,並確保 TypeScript 版本設為「使用最新可用版本」或至少設定為相當新的版本。我懷疑它目前設為 TypeScript 支援字串列舉之前的某個版本。

答案#2

我經歷了完全相同的行為,這似乎是 TypeScript 版本不相容的問題。與其按照上面建議將 TypeScript 版本更改為「使用最新可用版本」,我能夠通過將 TypeScript 版本更改為 2.2 來解決問題。