Fix plugin type declarations for NodeNext module resolution#135
Open
TrevorSayre wants to merge 1 commit into
Open
Fix plugin type declarations for NodeNext module resolution#135TrevorSayre wants to merge 1 commit into
TrevorSayre wants to merge 1 commit into
Conversation
ab69535 to
e1f76fe
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix plugin type declarations for NodeNext module resolution
Problem
When using
moduleResolution: NodeNext(orNode16) in TypeScript, importing a plugin and passing it toextend()produces a type error:Root Cause
The generated
.d.tsfiles for plugins useexport default function:Under
moduleResolution: NodeNext, TypeScript resolvesimport x from "module"to the module namespace type (thetypeof import(...)type) rather than the default export's type. This causes the function signature check to fail.Solution
Added a post-build script (
fix-plugin-types.mjs) that transforms the generated.d.tsfiles fromexport default functiontodeclare function+export =:With
export =, the module itself becomes the callable function type, eliminating the mismatch under NodeNext resolution.Why this approach?
export default functionexport =pattern is the TypeScript-recommended way to type CommonJS modules that export a single valuenpm run buildThe plugin source files were also updated to use
export default functiondeclarations instead ofconstarrow functions. This produces cleaner.d.tsfiles and makes the post-processing transformation straightforward.Without the post-build script, you'd need to either:
.d.tsfiles (error-prone)Changes Made
src/plugins/*.ts) to useexport default functiondeclarationsfix-plugin-types.mjspost-build script to transform plugin.d.tsfilespackage.jsonbuild script to run the fix after rollupNote on
fix-plugin-types.mjs: Uses ES modules (.mjs) for cleaner syntax. Requires Node.js 14+ (Node.js 12, the version before 14, was EOL on April 30, 2022) for native ESM support. This is a build-time script only, not shipped to users.Verification
moduleResolution: NodeNextin a separate project (no type errors)moduleResolutionvalues:node,bundler,classic,NodeNext,Node16So ultimately:
@ts-expect-error.Thank you for your consideration!
Related
typesexport condition to package.jsonNote on PR #118: That PR proposes restructuring the
package.jsonexports map (nestingtypesunderimport/require), but #95 already solved the exports issue with the standard TypeScript approach. #118 also does not address the core plugin type declaration issue. This PR fixes the remaining problem: theexport default functionpattern in.d.tsfiles still causes theTS2322type error under Node16/NodeNext module resolution. I fix that by transforming the generated declarations toexport =, which is the TypeScript-recommended pattern for callable module exports.