如何摆脱警告 .ts 文件是 TypeScript 编译的一部分,但未使用

How to get rid of the warning .ts file is part of the TypeScript compilation but it's unused

我刚刚将 angular 更新到最新的 9.0.0-next.4。我没有使用路由,但在更新后突然我不断看到这个警告。如何删除此警告

WARNING in src/war/angular/src/app/app-routing.module.ts is part of the TypeScript compilation but it's unused. Add only entry points to the 'files' or 'include' properties in your tsconfig.

package.json

  "dependencies": {
"@angular/animations": "^9.0.0-next.4",
"@angular/cdk": "^8.1.4",
"@angular/common": "^9.0.0-next.4",
"@angular/compiler": "^9.0.0-next.4",
"@angular/core": "^9.0.0-next.4",
"@angular/forms": "^9.0.0-next.4",
"@angular/material": "^8.1.4",
"@angular/platform-browser": "^9.0.0-next.4",
"@angular/platform-browser-dynamic": "^9.0.0-next.4",
"@angular/router": "^9.0.0-next.4",
"@ng-bootstrap/ng-bootstrap": "^5.1.0",
"bootstrap": "^4.3.1",
"hammerjs": "^2.0.8",
"moment": "^2.24.0",
"ng-image-slider": "^2.0.1",
"panzoom": "^8.1.2",
"rxjs": "~6.5.2",
"tslib": "^1.9.0",
"zone.js": "^0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.803.2",
    "@angular/cli": "^8.3.2",
    "@angular/compiler-cli": "^9.0.0-next.4",
    "@angular/language-service": "^9.0.0-next.4",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "^5.15.0",
    "typescript": "^3.5.3"
  }

tsconfig.json

    {
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

原来你需要从"include"中删除这一行 "src/**/*.ts" 来自 tsconfig.app.json 并且仅保留文件中的入口点(main.ts 和 polyfills.ts)

对我来说,问题是我使用的是:

loadChildren: () => import('./components/admin/_admin.module').then(m => m.AdminModule)

在我的 routes.ts 文件中,但没有导入模块。所以如果我只是把

import { AdminModule } from './components/admin/_admin.module';

它解决了。

我看到这些消息抱怨 environment.*.ts 文件,在从 Angular 8 升级到 Angular 9 包括 CLI 后,这些文件实际上在 angular.json 中提到了不同的构建本地和全球。但是,我没有 运行 ng update 可能会用以下内容更新 tsconfig.json,而是手动更新 tsconfig.json。

    "files": [
        "src/main.ts",
        "src/polyfills.ts"
    ],
    "include": [
        "src/**/*.d.ts"
    ]

然后警告消失。

更新 2020-05-27 Angular9.1.x Visual Studio Professional 2019

上面的小方块已经不需要了。否则,它会导致规范测试代码抱怨“找不到模块”,因为 ng test 正在构建并且 运行ning 很好,并且构建和 运行ning 确实存在ng 应用程序都可以。显然 NG 在 9 和 9.1 之间发生了一些变化。

这是我现在的工作 tsconfig.json:

{
    "compileOnSave": false,
    "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "module": "es2020",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es2015",
        "typeRoots": [
            "node_modules/@types"
        ],
        "lib": [
            "es2018",
            "dom"
        ],
        "skipLibCheck": true
    }
}

备注:

我只针对 Google Chrome 和 Safari,所以如果你想针对其他浏览器,你可能需要相应地调整。

我可以通过在 tsconfig.app.json 中定义 files 属性 来让它工作。这些文件是相对于 tsconfig.app.json 文件的。

"files": [
    "main.ts",
    "polyfills.ts"
  ]

这看起来很明显,但是对于您添加但尚未 referenced/imported 到另一个文件中的任何文件,您都会看到此警告。当您尝试编辑受警告影响的文件之一时,这将变得很明显,并且 Ivy 不会在编辑文件后自动重新编译。一旦将模块导入依赖文件并开始使用它,警告就会消失。

上面的答案可能与某些人有关,但我刚才在 post 中描述的是我发出警告的根本原因。请注意,我的 tsconfig.json 或 tsconfig.app.json 中没有 include 或 files 数组,当我在项目的其他地方实际引用这些文件时,警告就消失了。

今天更新到 Angular 9 并收到警告。 我的解决方案是添加此 "files" 数组,但路径中没有 "src"。刚刚添加:

 "files": [
    "main.ts",
    "polyfills.ts"
  ],

我的完整 tsconfig.app.json 文件是:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "types": ["node"]
  },
  "files": [
    "main.ts",
    "polyfills.ts"
  ],
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

我尝试了很多方法来解决这个问题,最后还是解决了。

我的问题是将在 angular 8.1 中找到的应用程序更新为 angular 9.x,但该应用程序还使用了 Ionic

你应该在 angular.json

里面只有 aot: true

在 src / polyfills.ts 中导入 './zone-flags.ts';删除.ts

https://medium.com/@grantbrits/upgrade-ionic-4-to-ionic-5-angular-76514079fb2aenter image description here

只需添加 zone-flags.ts 并删除所有包含。

 "files": [
    "src/main.ts",
    "src/polyfills.ts",
    "src/zone-flags.ts"
  ]

你在使用@angular-builders/custom-webpack吗?

我在 Angular10 中被这些消息轰炸了,以前从未见过它们。更改 includes 没有任何区别。

然后我找到了https://github.com/angular/angular/pull/36211

这基本上与这个问题中提出的错误相同,但对于 ngtypecheck.ts 文件(无论它们是什么 完全 我不确定!)

WARNING in /home/circleci/ng/aio/src/main.ngtypecheck.ts is part of the TypeScript compilation but it's unused. Add only entry points to the 'files' or 'include' properties in your tsconfig.

在我看来,这实际上与 @angular-builders/custom-webpack 有关。

https://github.com/just-jeb/angular-builders/issues/781 问题刚刚打开的地方。感谢 指出这一点。

更新到 v10.0.1 为我修复了它,但最新的问题请参阅上述问题。

"@angular-builders/custom-webpack": "10.0.1"    // as of today

检查您的主要 tsconfig.app.json 文件。

看看你里面有没有下面的内容,

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": []
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

最有可能的原因是这条线。

Add only entry points to the files or include properties in your tsconfig.

因此,如果 tsconfig.app.json 上有这些行,请将其删除。这就是错误所在:)

"exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]

我希望这会对某人有所帮助。

谢谢。

02-08-2020

离子 5+ Angular 9+ 应用

注意: 参见 include 部分。

tsconfig.app.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": []
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ],
  "exclude": [
    "src/**/*.spec.ts"
  ]
}

升级到 Angular 10 后,我收到了相同的警告。 运行 npm i 报告了某些开发依赖项的版本不匹配。升级这些 (npm i <package>@latest) 并将 nodejs 更新到版本 12(之前是版本 10)后,警告消失了。

就我而言,这些是必须更新的包:

  • @angular-devkit/build-angular
  • codelyzer

我在将 angular 9 应用程序更新到 angular 10 后遇到了同样的问题。更新 angular-devkit 解决了它。

在我的例子中,警告报告的类确实被使用了。但是,问题是导入它们时,它们的扩展名为“.js”。

因此,这一行导致错误“src\app\user 中的警告。service.ts 是 TypeScript 编译的一部分,但未使用”:

import { UserService } from './user.service.js';

我可以通过删除“.js”扩展名来修复它:

import { UserService } from './user.service';

在尝试了之前的解决方案建议后,它对我有用

在您的 package.json

中更新
"@angular-devkit/build-angular": "~0.1000.3" 

你能在这里看到所有版本吗?

https://www.npmjs.com/package/@angular-devkit/build-angular?activeTab=versions

检查哪个版本适用于您的当前版本 angular/core

for angular 10 使用版本 0.1000.3 of @angular-devkit/build-angular

我开始在 Angular 10 中看到警告,这有点令人惊讶,因为它是使用开箱即用的 CLI 创建的新应用程序,我在为test.tsenviornments.prod.ts。我认为默认情况下会排除这些文件,但事实并非如此,这很奇怪。

问题是这些文件不需要 TypeScript 转换;他们不需要 .js 版本的文件被捆绑并发送到浏览器。 test.tsenvironments.prod.ts 文件是结束 Angular 中构建时间要求的一种手段。因此,可以将它们添加到 tsconfig.app.json 中的 exclude 部分或应用程序中适用的 TypeScript 配置文件,如下所示:

  "exclude": [
    "src/**/*.spec.ts",
    "src/test.ts",
    "src/environments/environment.prod.ts"
  ]

如上添加后,警告将不再显示。

tsconfig.app.json

 "exclude": [
    "src/**/*.spec.ts",
    "src/test.ts",
    "src/environments/environment.prod.ts"
  ]

这个片段解决了这个问题。

我的团队几个月来一直在努力解决同样的问题,我终于发现我们的 tsconfig.worker.json 太包容了。在我们的案例中,显示了数百条“部分 TypeScript 编译但未使用”的消息——似乎应用程序中的每个 ts 文件都被列出,有些多次。

原来的样子:

"include": [
  "src/**/*.worker.ts"
]

是什么阻止了缺陷行为:

"files": [ ]   # no include or exclude block

认为 发生的事情是我们的标准 tsconfig.app.json 的 files 对象就足够了。仅从这些入口点 (main.ts & polyfills.ts),TS 编译器就能够找到对我们所有 *.worker.ts 文件的引用并转译它们。 tsconfig.webworker.ts 配置以某种方式告诉编译器两次查看所有 *.webworker.ts 文件。我不太清楚为什么会爆发出这么多警告;也许我们的 webworker 文件(我们有大约 20 个)不必要地导入了一堆他们不使用的东西。

无论如何,通过这个调整,我摆脱了无数的警告,我们的编译时间也减少了一半。呸!

在我的例子中,排除未使用的文件更容易:

"exclude": ["test.ts", "**/*.spec.ts","environments/environment.*.ts"]

在我的场景中,我的 tsconfig.json 缺少包含规范文件的排除项。这是我的 tsconfig 文件的片段,它解决了这个问题。

"exclude": [
"**/*.spec.ts",
"./node_modules/*"
],
 "files": [
"src/main.ts",
"src/polyfills.ts"
]

尝试删除包含 Angular CLI 缓存文件的文件夹 node_modules/.cli-ngcc