Repository navigation
Types are not compatible with moduleResolution: "NodeNext" #2032
Replies: 1 comment
|
Your diagnosis is right. I reproduced it with One thing worth knowing though: Until then, two workarounds that both type-check under NodeNext and call the real function at runtime (so no 1. Cast the default import (in ESM, the default import of a CJS module is import licenseCjs from 'rollup-plugin-license';
// Types claim a `default` export; at runtime the import *is* the function.
const license = licenseCjs as unknown as typeof licenseCjs.default;2. Load it with import { createRequire } from 'node:module';
import type licenseModule from 'rollup-plugin-license';
const license: typeof licenseModule.default =
createRequire(import.meta.url)('rollup-plugin-license');Both give you a fully typed |
Uh oh!
There was an error while loading. Please reload this page.
rollup-plugin-license@3.7.1doesn't work withmoduleResolution: "NodeNext".With the usual import:
TypeScript resolves the CommonJS module as an object instead of a function, so calling the
plugin fails and the build breaks. The function can be reached with a manual
.defaultaccess, but that's not a safe workaround: depending on how the module is loaded,
.defaultis
undefinedat runtime (seeFalseExportDefault).
The reason is a mismatch between the types and the runtime:
"main": "dist/index.js", noexportsfield, no"type": "module";dist/index.jsexports the plugin withmodule.exports = rollupPluginLicense;;dist/index.d.tsdeclares:For a CommonJS module,
export defaultin the.d.tsdescribesmodule.exports.default,which doesn't exist at runtime.
arethetypeswrong
confirms this - it reports "Incorrect default export" for all resolution kinds
(node10, node16 from CJS, node16 from ESM, bundler).
The types should use
export =instead:One caveat:
export =can't be combined with otherexportstatements, so the exportedtypes (
Options,Person,Dependency,ThirdParty, etc.) would need to be moved intoa
declare namespace rollupPluginLicenseblock.Would it be possible to fix this?
All reactions