Skip to content

type import("id") is translated to _$id with import * as _$id from "id" which is not an identical behavior #240

@guyutongxue

Description

@guyutongxue

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

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Contributions

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests (actually just go ahead and do it, thanks!)

Compensating engineering work will speed up resolution and support the project

  • I'm willing to offer $16 for financial support

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions