Skip to content

Commit d6de61d

Browse files
authoredOct 8, 2021
Fix shaking for functions types with overload signatures (#7036)
1 parent daf2cd9 commit d6de61d

File tree

7 files changed

+65
-5
lines changed

7 files changed

+65
-5
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Named1, Named2 } from "external";
2+
export function overloaded(arg: string): Named1;
3+
export function overloaded(arg: number): Named2;
4+
5+
//# sourceMappingURL=types.d.ts.map
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {Named1, Named2} from 'external';
2+
3+
export function overloaded(arg: string): Named1;
4+
export function overloaded(arg: number): Named2;
5+
export function overloaded(arg: unknown): unknown {
6+
return {};
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "ts-types-exporting-overload",
3+
"private": true,
4+
"main": "dist/main.js",
5+
"types": "dist/types.d.ts",
6+
"dependencies": {
7+
"external": "*"
8+
}
9+
}

‎packages/core/integration-tests/test/integration/ts-types/exporting-overload/yarn.lock

Whitespace-only changes.

‎packages/core/integration-tests/test/ts-types.js

+35
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,41 @@ describe('typescript types', function() {
135135
assert.equal(dist, expected);
136136
});
137137

138+
it('should generate ts declarations with export of an overloaded function signature', async function() {
139+
let b = await bundle(
140+
path.join(__dirname, '/integration/ts-types/exporting-overload/index.ts'),
141+
);
142+
143+
assertBundles(b, [
144+
{
145+
type: 'js',
146+
assets: ['index.ts'],
147+
},
148+
{
149+
type: 'ts',
150+
assets: ['index.ts'],
151+
},
152+
]);
153+
154+
let dist = (
155+
await outputFS.readFile(
156+
path.join(
157+
__dirname,
158+
'/integration/ts-types/exporting-overload/dist/types.d.ts',
159+
),
160+
'utf8',
161+
)
162+
).replace(/\r\n/g, '\n');
163+
let expected = await inputFS.readFile(
164+
path.join(
165+
__dirname,
166+
'/integration/ts-types/exporting-overload/expected.d.ts',
167+
),
168+
'utf8',
169+
);
170+
assert.equal(dist, expected);
171+
});
172+
138173
it('should generate ts declarations with externals', async function() {
139174
let b = await bundle(
140175
path.join(__dirname, '/integration/ts-types/externals/index.tsx'),

‎packages/transformers/typescript-types/src/TSModule.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type Export =
88
export class TSModule {
99
imports: Map<string, Import>;
1010
exports: Array<Export>;
11-
bindings: Map<string, any>;
11+
bindings: Map<string, Set<any>>;
1212
names: Map<string, string>;
1313
used: Set<string>;
1414

@@ -37,7 +37,9 @@ export class TSModule {
3737
}
3838

3939
addLocal(name: string, node: any) {
40-
this.bindings.set(name, node);
40+
const bindings = this.bindings.get(name) ?? new Set();
41+
bindings.add(node);
42+
this.bindings.set(name, bindings);
4143
if (name !== 'default') {
4244
this.names.set(name, name);
4345
}

‎packages/transformers/typescript-types/src/TSModuleGraph.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ export class TSModuleGraph {
6464
return ts.visitEachChild(node, visit, context);
6565
};
6666

67-
let node = module.bindings.get(name);
68-
if (node) {
69-
ts.visitEachChild(node, visit, context);
67+
let bindings = module.bindings.get(name);
68+
if (bindings) {
69+
for (let node of bindings) {
70+
ts.visitEachChild(node, visit, context);
71+
}
7072
}
7173
}
7274

0 commit comments

Comments
 (0)
Please sign in to comment.