Skip to content

fix: handle function overloads in .d.ts bundling#210

Open
hugs7 wants to merge 1 commit into
sxzz:mainfrom
hugs7:fix/duplicated-export-function-overloads
Open

fix: handle function overloads in .d.ts bundling#210
hugs7 wants to merge 1 commit into
sxzz:mainfrom
hugs7:fix/duplicated-export-function-overloads

Conversation

@hugs7
Copy link
Copy Markdown

@hugs7 hugs7 commented Mar 19, 2026

fix: handle function overloads in .d.ts bundling

Fixes #209

Problem

When isolatedDeclarations is not enabled, rolldown-plugin-dts uses tsc to generate .d.ts files. For function overloads, tsc correctly emits multiple export declare function declarations with the same name:

export declare function useConfig(): Config;
export declare function useConfig<T>(selector: (config: Config) => T): T;

During the transform phase, the fake-js plugin converts each declaration into a var useConfig = [id, deps, children] assignment. With overloads, this produces two export var useConfig statements, which Rolldown's parser rejects as duplicate exports.

Solution

The fake-js plugin now detects function overload signatures during transform and merges them into the primary declaration:

  • Transform phase: When a TSDeclareFunction is encountered whose binding name has already been processed, the overload's declaration, deps, params, and children are stored on the existing DeclarationInfo rather than emitting a new runtime variable. The overload's deps/params/children are appended to the primary's arrays so they go through Rolldown's identifier renaming pipeline.

  • RenderChunk phase: When restoring a declaration that has overloads, all overload signatures are emitted alongside the primary declaration in original source order, with proper identifier renaming applied via the merged arrays.

Test

Added a function-overloads test fixture that verifies both overload signatures appear in the bundled output:

interface Config {
  name: string
  version: number
}

export function useConfig(): Config
export function useConfig<T>(selector: (config: Config) => T): T
export function useConfig<T>(selector?: (config: Config) => T) {
  const config: Config = { name: 'app', version: 1 }
  return selector ? selector(config) : config
}

When tsc emits multiple `export declare function` declarations with
the same name (function overloads), Rolldown's parser rejected them as
duplicate exports. The fake-js transform now detects overload signatures,
merges their deps/params/children into the primary declaration for proper
identifier renaming, and restores all overloads in the correct order
during renderChunk.

Fixes sxzz#209

Amp-Thread-ID: https://ampcode.com/threads/T-019d0682-c080-7508-81b0-e69e1b444bb7
Co-authored-by: Amp <amp@ampcode.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicated export when bundling .d.ts with function overloads

1 participant