Skip to content
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

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

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

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

remcohaszing opened this issue May 26, 2023 · 2 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?

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

3 participants