You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular12项目中tsconfig的ExperimentalDecorators配置未生效问题

Angular 11升级至12后装饰器警告及node_modules类型错误问题

升级Angular 11项目到Angular 12后,遇到以下问题:

  • 反复出现警告:装饰器的实验性支持是一项未来版本可能变更的特性,请在'tsconfig'或'jsconfig'中设置'experimentalDecorators'选项以消除此警告。
  • 出现大量来自node_modules/typescript/lib文件的类型错误

已执行的操作:

  • 更新了所有项目依赖
  • 在tsconfig.json中配置了"experimentalDecorators": true和"allowJs": true,但配置未生效,警告和错误仍存在
  • 项目为嵌套目录结构,tsconfig.json位于内部目录,与src文件夹同级

项目package.json内容

{
  "name": "ota-dashboard",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --base-href=/main/ --proxy-config proxy.conf.json --port=4201",
    "build": "ng build --configuration production --base-href=/main/",
    "test": "ng test --code-coverage",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "ng-build-all": "ng build otalib && ng build"
  },
  "private": true,
  "dependencies": {
    "@angular-material-components/datetime-picker": "^6.0.3",
    "@angular/animations": "^12.2.17",
    "@angular/cdk": "^12.2.13",
    "@angular/common": "~12.2.17",
    "@angular/compiler": "~12.2.17",
    "@angular/core": "~12.2.17",
    "@angular/forms": "~12.2.17",
    "@angular/localize": "^12.2.17",
    "@angular/material": "^12.2.13",
    "@angular/platform-browser": "~12.2.17",
    "@angular/platform-browser-dynamic": "~12.2.17",
    "@angular/router": "~12.2.17",
    "@ngx-formly/bootstrap": "^5.0.0",
    "@ngx-formly/core": "^5.0.0",
    "@ngx-translate/core": "^13.0.0",
    "@ngx-translate/http-loader": "^6.0.0",
    "ajv": "^6.10.0",
    "angular2-query-builder": "^0.6.2",
    "bootstrap": "^4.5.2",
    "chart.js": "^2.9.4",
    "classlist.js": "1.1.20150312",
    "core-js": "^3.8.1",
    "file-saver": "^2.0.5",
    "material-icons": "^0.3.1",
    "ng2-charts": "^2.4.2",
    "ngx-guided-tour": "^1.1.11",
    "ngx-spinner": "^12.0.0",
    "rxjs": "~6.6.0",
    "spark-md5": "^3.0.2",
    "tslib": "^2.3.1",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-builders/custom-webpack": "^12.1.3",
    "@angular-devkit/build-angular": "~12.2.18",
    "@angular/cli": "~12.2.18",
    "@angular/compiler-cli": "~12.2.17",
    "@ngx-formly/schematics": "^5.10.22",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "compression-webpack-plugin": "^6.0.5",
    "jasmine-core": "~3.8.0",
    "jasmine-spec-reporter": "~6.0.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.7.0",
    "ng-packagr": "^12.2.7",
    "protractor": "~7.0.0",
    "puppeteer": "^10.4.0",
    "ts-node": "~8.3.0",
    "typescript": "~4.2.4",
    "webpack-merge": "^5.8.0"
  }
}

项目tsconfig.json内容

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowJs": true,
    "importHelpers": true,
    "target": "es5",
    "module": "esnext",
    "types": ["node"],
    "skipLibCheck": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ],
    "paths": {
      "otalib": [
        "dist/otalib/otalib",
        "dist/otalib"
      ],
      "core-js/es6/": [
        "./node_modules/core-js/es/"
      ],
      "*": [
        "*",
        "generated/*"
      ]
    }
  },
  "include": [
    "./src/**/*",
    "./src/**/*.d.ts"
  ],
  "exclude": [
    "./node_modules",
    "./node_modules/typescript/lib/lib.dom.d.ts",
    "./node_modules/typescript/lib/lib.webworker.d.ts"
  ]
}

已尝试在tsconfig.json中添加以下配置以消除警告,但未生效:

"experimentalDecorators": true,
"allowJs": true

项目文件夹结构说明:采用嵌套目录,tsconfig.json与src目录处于同一层级,错误集中在typescript库文件中。


内容的提问来源于stack exchange,提问作者Satyam Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 19:05:24