Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
5 tasks done
remcohaszing opened this issue May 26, 2023 · 3 comments
Open
5 tasks done

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

remcohaszing opened this issue May 26, 2023 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@remcohaszing
Copy link

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.

@RyanCavanaugh
Copy link
Member

The intended configuration solution for this is project references, but I can appreciate how this is a simple approach to the same problem.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels May 26, 2023
@jsgoupil
Copy link

I have a feeling this is exactly the problem I am hitting... I would love to be able to "Find All References", but I believe it is excluded.

With the following:

tsconfig.json

{
  "files": [],
  "include": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ]
}

tsconfig.app.json

{
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "module": "commonjs"
  },
  "exclude": ["**/*.spec.ts"],
  "include": ["**/*.ts"]
}

tsconfig.spec.json

{
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "module": "commonjs"
  },
  "include": ["**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"]
}

Doing a "Find all references" in a .ts file will NOT include references found in .spec.ts files.

If I open the spec file, now all references of that file are found.
It seems there is no way to support this scenario properly. Maybe this type checking proposed in this bug would help?

@GermanJablo
Copy link

Since the files to be compiled are usually a subset of those being type-checked, I suggest introducing an excludeFromBuild option that specifically narrows down the exclude scope.

This way, a single property ensures that the set of files to be compiled is a subset of the files to be typechecked, which greatly simplifies things.

The most common way this problem is currently solved is with a tsconfig.json for typecheck, and a tsconfig.build.json for compile. But this approach introduces several pitfalls:

  • It's easy for both files to end up out of sync with different properties.
  • It's easy to accidentally overwrite existing globs when trying to extend include, exclude, or rootDir.
  • It's easy to forget to use -p tsconfig.build.json in some script, incorrectly using tsconfig.json, intended for typechecking.
  • Due to the lack of a standard or recommended approach, many mistakenly use tsconfig.json for builds and extend a tsconfig.typecheck.json for type-checking, which can cause problems with other tools, such as eslint.

For these reasons, many people, even TypeScript experts, avoid typechecking files that will not be compiled (configuration files, scripts, tests, etc.), or they do so with JSDoc annotations. I can see this happening even in the monorepo templates well well-maintained by turborepo.

The intended configuration solution for this is project references

@RyanCavanaugh I've read all the project references documentation and I don't see how they could solve this problem. Is there something I'm missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants