Skip to content

fix: re-export ESM types in CommonJS#416

Merged
nzakas merged 3 commits intomainfrom
reexport-types
Apr 1, 2026
Merged

fix: re-export ESM types in CommonJS#416
nzakas merged 3 commits intomainfrom
reexport-types

Conversation

@fasttime
Copy link
Copy Markdown
Member

@fasttime fasttime commented Mar 25, 2026

Prerequisites checklist

AI acknowledgment

  • I did not use AI to generate this PR.
  • (If the above is not checked) I have reviewed the AI-generated content before submitting.

What is the purpose of this pull request?

This PR simplifies CommonJS types by re-exporting ESM types.

Most packages in this repo export types both in CommonJS and ESM format, which is necessary for compatibility with both module systems. Today, the CommonJS declarations duplicate the ESM declarations with only small replacements to match import paths. For example, these are the ESM type declarations for @eslint/[email protected]:

And these are the corresponding, very similar CommonJS declarations:

To avoid the duplication and reduce the published package size, this PR changes the CommonJS type declarations to one single file that just re-exports the ESM types:

// dist/cjs/index.d.cts
import type * as types from "../esm/index.js" with { "resolution-mode": "import" };
export = types;

This is now possible thanks to the resolution-mode import attribute, which has been supported since TypeScript 5.3, the earliest TypeScript version supported by ESLint.

This change affects all packages that use the build:cts script, i.e.:

  • compat
  • config-array
  • config-helpers
  • core
  • object-schema
  • plugin-kit

What changes did you make? (Give an overview)

  • Updated tools/build-cts.js script re-exports ESM type declarations instead of duplicating them.
  • Updated Rollup configurations so that dist/cjs/types.cts files are no longer created.
  • Updated build:cts script in core package to use the new script.

Related Issues

refs eslint/eslint#19920

Is there anything you'd like reviewers to focus on?

@fasttime fasttime marked this pull request as ready for review March 26, 2026 05:53
@mdjermanovic
Copy link
Copy Markdown
Member

Will this also solve problems we had in eslint/eslint#19843, since both ESM and CJS entry points of @eslint/core will return types declared in the same file?

@mdjermanovic mdjermanovic moved this from Needs Triage to Implementing in Triage Mar 27, 2026
Copy link
Copy Markdown
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! Would like @nzakas to verify before merging.

If this solves eslint/eslint#19843 and generally unifies types for different entry points, perhaps we should tag it as feat or at least fix?

@mdjermanovic mdjermanovic moved this from Implementing to Second Review Needed in Triage Mar 27, 2026
@fasttime
Copy link
Copy Markdown
Member Author

Will this also solve problems we had in eslint/eslint#19843, since both ESM and CJS entry points of @eslint/core will return types declared in the same file?

Now that you mention it, it seems so! I can see that with this change, interface augmentation for rules and settings works also in ESM. For example, currently this config is not producing any errors:

But if the CJS types are patched like in this PR, TypeScript recognizes the type mismatches and reports the expected errors:

That means we could resume working on eslint/eslint#19843 once this change is released.

@mdjermanovic
Copy link
Copy Markdown
Member

Great! Then, I think we can consider this a fix since it unifies exported types so I'm changing the tag to fix. Also to trigger releases.

@mdjermanovic mdjermanovic changed the title refactor: re-export ESM types in CommonJS fix: re-export ESM types in CommonJS Mar 30, 2026
@eslint-github-bot eslint-github-bot Bot added the bug Something isn't working label Mar 30, 2026
Copy link
Copy Markdown
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@nzakas nzakas merged commit ef16f80 into main Apr 1, 2026
32 checks passed
@nzakas nzakas deleted the reexport-types branch April 1, 2026 20:29
@github-project-automation github-project-automation Bot moved this from Second Review Needed to Complete in Triage Apr 1, 2026
@eslintbot eslintbot mentioned this pull request Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working chore

Projects

Status: Complete

Development

Successfully merging this pull request may close these issues.

3 participants