Skip to content

Invalid generated .d.ts when typeof undefined is used as type #171

@BlueGreenMagick

Description

@BlueGreenMagick

Reproduction link or steps

https://github.com/BlueGreenMagick/tsdown_typeof_undefined

Describe the bug

rolldown-plugin-dts(v0.21.5) does not compile typeof undefined usage as a type correctly.

export function typeof_undefined(): typeof undefined {
  return undefined;
}

This is transformed into below .d.ts code which the typescript library(v5.9.3) fails to parse.

declare function typeof_undefined(): typeof void 0;

Various error may appear depending on context:

TS1144: '{' or ';' expected.
TS1005: ';' expected.

Apparantly, typeof undefined is a valid type in typescript because undefined is a global object. (ECMA spec)
You can test this out by opening a browser and running window.undefined: you'll get the value undefined

Found this error when using tsdown to build the library acorn-typescript (problematic code).

I think it was probably a typo - I can't think of any valid reasons to use typeof undefined over undefined.

System Info

System:
    OS: macOS 15.6
    CPU: (8) arm64 Apple M1
    Memory: 351.30 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 25.2.1 - /opt/homebrew/bin/node
    npm: 11.6.2 - /opt/homebrew/bin/npm
    pnpm: 10.23.0 - /opt/homebrew/opt/node@22/bin/pnpm
  Browsers:
    Chrome: 144.0.7559.97
    Firefox: 147.0.1
    Safari: 18.6

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

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions