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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/57729518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:51:07  来源:igfitidea点击:

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

angulartypescriptangular-cli

提问by dota2pro

I Just updated angular to latest 9.0.0-next.4. I am not using routing but suddenly after updating I keep seeing this warning. How Do I remove this warning

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

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

WARNING insrc/war/angular/src/app/app-routing.module.ts是 TypeScript 编译的一部分,但未使用。仅将入口点添加到 tsconfig 中的“文件”或“包含”属性。

package.json

包.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"
    ]
  }
}

采纳答案by Ayyash

It turned out that you need to remove this line from "include" "src/**/*.ts"from tsconfig.app.jsonand only keep entry points in files (main.ts and polyfills.ts)

事实证明,您需要从“include”中删除这一行 "src/**/*.ts"tsconfig.app.json并且只在文件(main.ts 和 polyfills.ts)中保留入口点

回答by emp

I could get it working by defining the filesproperty in tsconfig.app.json. These files are relative to the tsconfig.app.jsonfile.

我可以通过filestsconfig.app.json. 这些文件是相对于tsconfig.app.json文件的。

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

回答by ZZZ

I had seen these messages complaining about environment.*.ts files which are actually mentioned in angular.json for different builds, after upgrading from Angular 8 to Angular 9 including CLI local and global. However, I did not run ng updatewhich might update tsconfig.json with the following, instead I updated packages.json manually.

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

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

Then the warnings disappear.

然后警告消失。

Update 2020-05-27 with Angular 9.1.x in Visual Studio Professional 2019

在 Visual Studio Professional 2019 中使用 Angular 9.1.x 更新 2020-05-27

The little block above is not needed anymore. Otherwise, it will cause the spec test codes complaining "module not found" against modules which are actually there since ng testis building and running just fine, and the build and the running of the ng app are OK. Apparently somethings in NG had changed between 9 and 9.1.

不再需要上面的小块了。否则,它将导致规范测试代码针对实际存在的模块抱怨“找不到模块”,因为ng test构建和运行良好,并且 ng 应用程序的构建和运行正常。显然,NG 中的某些内容在 9 和 9.1 之间发生了变化。

Here's my working tsconfig.json now:

这是我现在工作的 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
    }
}

remarks:

评论:

I target Google Chrome and Safari only, so if you want to target other browsers, you may need to adjust accordingly.

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

回答by fabio984

Updated to Angular 9 today and got warnings. My solution was add this "files" array without the "src" in the path. Just added:

今天更新到 Angular 9 并收到警告。我的解决方案是添加这个“文件”数组,路径中没有“src”。刚刚添加:

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

My full tsconfig.app.json file is:

我的完整 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"
  ]
}

回答by Matthew Pitts

This may seem obvious, BUT you will see this warning for any file that you add but is not yet referenced/imported into another file. This will become obvious when you attempt to edit one of the files subject to the warning, and Ivy does not automatically recompile after editing the file. Once you import the module into a dependent file and start using it, the warnings go away.

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

The answers above may be relevant to some, but what I just described in this post was the root cause of my warnings. Note, I do not have an include or files array in my tsconfig.json or tsconfig.app.json and the warnings went away as soon as I actually referenced the files elsewhere in my project.

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

回答by Elkin Gutierrex

I tried many things to solve this problem, and in the end I was able to solve it.

我尝试了很多方法来解决这个问题,最终我能够解决它。

my problem was updating an app that was found in angular 8.1 to angular 9.x, but the app also used Ionic

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

you should only have aot: trueinside angular.json

你应该只有aot: true里面angular.json

in src / polyfills.ts to import './zone-flags.ts'; remove .ts

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

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

https://medium.com/@grantbrits/upgrade-ionic-4-to-ionic-5-angular-76514079fb2a在此处输入图片描述

回答by user12627115

For me, the problem was that I was using:

对我来说,问题是我正在使用:

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

in my routes.ts file but wasn't importing module. So if I just put

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

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

it solves it.

它解决了它。