Skip to content

Add option to include files in type checking, but not compilingΒ #54410

Open
@remcohaszing

Description

@remcohaszing

Suggestion

πŸ” Search Terms

rootDir exclude

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I would love to have an option to include files for type checking, but not compiling.

One solution would be to add an option (checkOutsideRootDir?) to support type checking files outside the rootDir. An alternative way could be to provide separate glob patterns for type checking and compiling.

πŸ“ƒ Motivating Example

Let’s say you are testing you project with Vitest. You probably have a file structure that looks something like this:

β”œβ”€β”€ package.json
β”œβ”€β”€ src
β”‚Β Β  └── index.ts
β”œβ”€β”€ tests
β”‚Β Β  └── index.test.ts
β”œβ”€β”€ tsconfig.json
└── vitest.config.ts

Previously you needed two TypeScript configuration files, one for type checking everything, and one for compiling your source code. With this new option enabled, TypeScript will type check all TypeScript files, but it will only compile the files from the rootDir directory.

{
  "compilerOptions": {
    "checkOutsideRootDir": true,
    "rootDir": "src",
    "outDir": "dist"
  }
}

πŸ’» Use Cases

Currently I feel like there is no proper way to configure TypeScript for both type checking and compiling. Some special configuration is needed to compile source code, and type check tests / config files, but exclude those from the build.

Often repos end up with tsconfig.json and something along the way of tsconfig.build.json, tsconfig-prod.json, dev.tsconfig.json, or something along those lines. It’s not consistent across the ecosystem. This file then uses a mix of extends / include / exclude to create complex configurations. Alternatively people just exclude certain files, not being aware they are being excluded from type checking as well. Or they use an alternative tool for building.

Regardless of what people come up with, it often leads to awkward configurations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions