Reproduction link or steps
Reproducible demo:
https://stackblitz.com/edit/stackblitz-starters-vwzwun5x?file=verify_mismatch.ts
Steps:
- Declares
"foo" as a CommonJS library with export = namespace syntax.
- Setup
package.json with type=module, tsconfig.json with module=NodeNext and isolateDeclarations=true.
- Write code
type FooModule = typeof import("foo");
- The above code will be transformed by
rolldown-plugin-dts to:
import * as _$foo from "foo";
type FooModule = typeof _$foo;
- This causes wrong typing results, since
typeof _$foo will attach a default property under NodeNext module settings (see below).
Describe the bug
The typing of a namespace-imported CJS is related to tsconfig's module. If we configured module to node*, then typescript will truly reflect runtime behavior of importing CJS to ESM -- mapping module.exports to default property of an ECMAScript Module project. But an import("xxx") still gots CJS typings which will NOT have a default property, so type mismatch happened.
The current transform behavior (defined in
|
function importNamespace( |
) will cause an additional
default property produced after bundling declaration. This may cause following issue:
// `create-foo` returns `import("foo")`, not transformed by rolldown-plugin-dts
import { createFoo } from "create-foo";
// `useFoo` accepts `import("foo")` originally, but transformed to accepts `_$foo` instead
import { useFoo } from "use-foo";
useFoo(createFoo());
// ^~ typeof import("foo") missed the property `default` which is required by `typeof _$foo`
System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 22.22.0 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 10.8.2 - /usr/local/bin/npm
pnpm: 8.15.6 - /usr/local/bin/pnpm
Validations
Contributions
Compensating engineering work will speed up resolution and support the project
Reproduction link or steps
Reproducible demo:
https://stackblitz.com/edit/stackblitz-starters-vwzwun5x?file=verify_mismatch.ts
Steps:
"foo"as a CommonJS library withexport = namespacesyntax.package.jsonwithtype=module,tsconfig.jsonwithmodule=NodeNextandisolateDeclarations=true.type FooModule = typeof import("foo");rolldown-plugin-dtsto:typeof _$foowill attach adefaultproperty underNodeNextmodule settings (see below).Describe the bug
The typing of a namespace-imported CJS is related to
tsconfig'smodule. If we configuredmoduletonode*, then typescript will truly reflect runtime behavior of importing CJS to ESM -- mappingmodule.exportstodefaultproperty of an ECMAScriptModuleproject. But animport("xxx")still gots CJS typings which will NOT have adefaultproperty, so type mismatch happened.The current transform behavior (defined in
rolldown-plugin-dts/src/fake-js.ts
Line 725 in 7776dfa
defaultproperty produced after bundling declaration. This may cause following issue:System Info
System: OS: Linux 5.0 undefined CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: 1.0 - /bin/jsh Binaries: Node: 22.22.0 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 10.8.2 - /usr/local/bin/npm pnpm: 8.15.6 - /usr/local/bin/pnpmValidations
Contributions
Compensating engineering work will speed up resolution and support the project