Skip to content

Conversation

@rekmarks
Copy link
Member

@rekmarks rekmarks commented Oct 24, 2024

It appears that a better world is possible: we can both use Node16 module resolution for builds and test, lint, and develop directly from our source files.

@sirtimid previously fixed #174 and #181 over several PRs: #192 #193 #194 #195

In #194, we concluded that there was no way to run type checks without building our source files. As it turns out, this is in fact possible by disabling the composite config option1. Having done this, we now run lint:ts as part of our regular lint scripts, which reliably works without building. In addition, this also fixes type errors in our vitest.config.ts files.

So that we have this documented, let's review our config to understand why our new config works.

In our root tsconfig.json and tsconfig.package.json (which package-level tsconfig.json files extend), we set the following:

{
    "module": "Preserve",
    "moduleResolution": "Node10",
}

Using Node10 for moduleResolution causes TypeScript to ignore the exports field of package.json, which enables our paths config in tsconfig.packages.json to correctly resolve our source files. Using Preserve for module simply appears to be compatible with our config. Other options, like ES2015, worked as well, but from the description of the setting it sounds like what we want.

Meanwhile, our tsconfig.test.json files have been renamed to tsconfig.lint.json, and they now look like this:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "composite": false,
    "noEmit": true,
    "skipLibCheck": true
  },
  "include": ["./src"],
  "exclude": []
}

Due to the semantics of extends, references is omitted, and include and exclude overwrite their inherited values. skipLibCheck ensures that we ignore errors in dependencies, and noEmit of course ensures that we don't actually produce any output files.

We still use Node16 as our module and moduleResolution options for actual builds, as is recommended by ts-bridge. There may come a time when this discrepancy causes trouble for us, but for now, it appears that we are in a happy place.

Footnotes

  1. Boundless gratitude to https://github.com/microsoft/TypeScript/issues/27069#issuecomment-2415291501 for this insight

@rekmarks rekmarks requested a review from a team as a code owner October 24, 2024 20:39
@rekmarks
Copy link
Member Author

For posterity, I'll note that we considered the following addition to our base Vitest config, but it doesn't work in @ocap/extension, the lint:ts scripts are fast, and we are done fidgeting with this.

test: {
  // ...
  typecheck: {
    enabled: true,
    ignoreSourceErrors: false,
    tsconfig: '../tsconfig.lint.json',
    include: ['**/*.test.ts'],
  },
}

@rekmarks rekmarks merged commit 466a4c4 into main Oct 24, 2024
@rekmarks rekmarks deleted the rekm/sane-typechecks-are-possible branch October 24, 2024 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Configure TypeScript to use source files during test and development time

3 participants