Skip to content

vitest typecheck does not work when TypeScript references are used with noEmit #3752

@scottwillmoore

Description

@scottwillmoore

Describe the bug

I use TypeScript references in my Vite projects as recommended in their official templates. For example, the React with TypeScript template creates both a tsconfig.json and a tsconfig.node.json, which are combined with the use of TypeScript references. This allows the web files and the configuration files to receive different compilerOptions. In my projects I tend to create a minimal tsconfig.json which references tsconfig.node.json and tsconfig.web.json.

The problem occurs when references are used with noEmit such as when vitest executes tsc --noEmit ... to test the types of the project. This doesn't check any files declared in the references. In addition, you appear to get this error: TS6305: Output file '.../index.d.ts' has not been built from source file '.../index.ts'.

This is a documented, and known issue with the TypeScript compiler. It also doesn't look like it is going to be resolved...

To fix this, at the moment there are two solutions:

  1. Don't use TypeScript references for important types that need to be checked. For example, instead of using a minimal tsconfig.json with tsconfig.node.json and tsconfig.web.json, I decided to merge my tsconfig.web.json back into my tsconfig.json. Although not ideal, I think this should cover most instances of this problem.

  2. Use tsc --build without noEmit. You should setup an outDir so it doesn't pollute your source files. In addition, it is wasteful to build the project when you already use Vite, or another bundler... This would require an update to Vitest in order to be able to use vitest typecheck again.

Reproduction

I don't have time to create a perfect reproduction of the issue, but will work on it when I have some more free time. I thought I would create this issue to help others discover and understand this problem.

For now, you could use this project at 70387a6, and run npm run test:types in the packages/app directory.

I have now created a better reproduction: https://github.com/scottwillmoore/vitest-with-typescript-references!

System Info

System:
    OS: Linux 5.15 NixOS 23.05 (Stoat) 23.05 (Stoat)
    CPU: (6) x64 Intel(R) Core(TM) i5-8600K CPU @ 3.60GHz
    Memory: 4.63 GB / 7.73 GB
    Container: Yes
    Shell: 5.2.15 - /run/current-system/sw/bin/bash
  Binaries:
    Node: 18.16.1 - /nix/store/l43bdmxxlkrzwic3q24ik366qyqg3s1g-nodejs-18.16.1/bin/node
    npm: 9.5.1 - /nix/store/l43bdmxxlkrzwic3q24ik366qyqg3s1g-nodejs-18.16.1/bin/npm

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: typecheckIssues and PRs related to typechecking featurep3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions