microsoft / microsoft/TypeScript

Duplicate function declarations in generated `.d.ts` for non-module JS files with `allowJs` + `declaration`

Open
#63,546 3 comments 0 reactions 0 assignees View on GitHub
Bug Domain: Declaration Emit
Dominant language
Go
Stars
111k
Forks
14.3k
PR merge metrics
PR metrics pending

Description

### 🔎 Search Terms

allowJs declaration duplicate, allowJs declaration emit multiple files global function, declaration emit global scope merged symbol resolveAnonymousTypeMembers, script file declaration emit duplicates

### 🕗 Version & Regression Information

This is the behavior in every version I tried, including 6.0.3 (also tested typescript@next)

Playground Link: requires multi-file setup with tsconfig.json.

I used stackblitz instead.

### ⏯ Playground Link

https://stackblitz.com/edit/node-emxsybj1?file=tsconfig.json

### 💻 Code

**tsconfig.json**

```json
{
"compilerOptions": {
"allowJs": true,
"declaration": true,
"emitDeclarationOnly": true
},
"include": ["*.js"]
}
```

**a.js**

```js
/** @filename: a.js */
function func(x) {
return x + 1;
}
```

**b.js**

```js
/** @filename: b.js */
function func(x) {
return x + 2;
}
```

### 🙁 Actual behavior

**a.d.ts** and **b.d.ts** each contain the function declaration twice (once from each file).

**a.d.ts**

```ts
/** @filename: a.js */
declare function func(x: any): any;
declare function func(x: any): any;
```

**b.d.ts**

```ts
declare function func(x: any): any;
/** @filename: b.js */
declare function func(x: any): any;
```

### 🙂 Expected behavior

Each **.d.ts** should only contain the declaration from its own source file or error (not sure).

### Additional information about the issue

related issues:

- #27011
- #49200

**EDIT1:** updated _Expected behavior_

Contributor guide

Open the contributing guide

Research direction

Start by running tsc with the reported tsconfig.json and the two-file a.js/b.js reproduction, then trace declaration emit for global symbols and the resolveAnonymousTypeMembers path mentioned in the search terms. Compare the output with issues #27011 and #49200. Done means each generated .d.ts contains only its own declaration, or the compiler reports a clear error if that behavior is invalid.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.