Resolving TypeScript Compilation Warnings: Unused .ts Files Issue

Dec 03, 2025 · Programming · 11 views · 7.8

Keywords: TypeScript Compilation Warning | Angular 9 | tsconfig Configuration

Abstract: This article provides an in-depth analysis of TypeScript compilation warnings that occur after updating to Angular 9, where certain .ts files are included in compilation but remain unused. Based on the best answer, it explains how to eliminate these warnings by modifying the tsconfig.app.json configuration file, including removing unnecessary include patterns or explicitly specifying files entry points. The article explores core concepts of TypeScript compilation configuration, such as the differences between files and include properties, and the impact of Angular CLI project structure on the compilation process. Through code examples and step-by-step guidance, it helps developers understand and resolve similar configuration issues, ensuring clean and efficient project builds.

In Angular development, after upgrading to new versions, developers may encounter various compilation warnings, one of which is a TypeScript compilation warning indicating that certain .ts files are included in the compilation process but are not actually used. This warning often appears in Angular 9 and above, especially when routing functionality is not utilized in the project. This article builds on a specific case study to delve into the root causes of this issue, provide solutions, and discuss best practices for TypeScript configuration.

Problem Background and Warning Analysis

When developers upgrade an Angular project to version 9.0.0-next.4, they might see the following warning during the build process: 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. This warning indicates that the app-routing.module.ts file is included by the TypeScript compiler, but since routing is not used in the project, the file is not referenced during compilation, leading to redundancy. The core issue lies in the TypeScript compilation configuration, particularly improper settings of the files and include properties in tsconfig.json or its derivative files.

TypeScript Compilation Configuration Explained

In Angular projects, TypeScript compilation is configured through the tsconfig.json file. Angular CLI typically generates multiple tsconfig files, such as tsconfig.app.json for application builds and tsconfig.spec.json for testing. These files inherit from the root tsconfig.json and can override or extend its settings. Key properties include:

In the provided case, tsconfig.app.json might contain an include pattern like "src/**/*.ts", causing all .ts files to be compiled, including unused ones like app-routing.module.ts. This violates TypeScript best practices, which advocate compiling only necessary files to improve build efficiency and reduce warnings.

Solution: Modifying tsconfig.app.json

According to the best answer, the key to resolving this warning is adjusting the configuration in tsconfig.app.json. There are two main approaches:

  1. Remove Unnecessary Include Patterns: If tsconfig.app.json includes "src/**/*.ts", it should be removed to avoid compiling all .ts files. For example, a modified configuration might look like:
    {
      "extends": "./tsconfig.json",
      "compilerOptions": {
        "outDir": "./out-tsc/app",
        "types": []
      },
      "files": [
        "src/main.ts",
        "src/polyfills.ts"
      ],
      "include": [
        "src/**/*.d.ts"
      ]
    }
    Here, the files property specifies entry points main.ts and polyfills.ts, while include only includes type definition files, thus precisely controlling the compilation scope.
  2. Explicitly Specify Files Entry Points: As an alternative, rely entirely on the files property to list all files to compile, without using include. For example:
    {
      "extends": "./tsconfig.json",
      "compilerOptions": {
        "outDir": "./out-tsc/app",
        "types": []
      },
      "files": [
        "src/main.ts",
        "src/polyfills.ts"
      ]
    }
    This method is more direct but requires ensuring all dependency files are implicitly or explicitly included.

After implementing these changes, rerun the build command (e.g., ng build), and the warning should disappear. If the project uses Angular CLI's default configuration, tsconfig.app.json is usually auto-generated, but upgrades may leave old settings, so manual checks are necessary.

Supplementary Insights from Other Answers

Other answers provide additional perspectives:

Overall, the core principle is to keep compilation configurations concise and targeted. Avoid overusing glob patterns and instead explicitly specify compilation entries based on the project structure. In the Angular ecosystem, following CLI-recommended configurations can reduce such issues.

Best Practices and Conclusion

To avoid similar warnings, developers should adopt the following measures:

Through this analysis, developers can gain a deeper understanding of TypeScript compilation mechanisms and effectively resolve configuration warnings in Angular projects. This not only enhances build efficiency but also promotes cleaner code management practices.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.