Skip to content

Commit d528c69

Browse files
committed
fix: Use type nodes if converting a regular function
Fixes some instances of TypeDoc falling into the type interning trap Closes #1454
1 parent 6027ba8 commit d528c69

File tree

5 files changed

+187
-13
lines changed

5 files changed

+187
-13
lines changed

src/lib/converter/factories/signature.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ export function createSignature(
6868
} else {
6969
sigRef.type = context.converter.convertType(
7070
context.withScope(sigRef),
71-
signature.getReturnType()
71+
(declaration?.kind === ts.SyntaxKind.FunctionDeclaration &&
72+
declaration.type) ||
73+
signature.getReturnType()
7274
);
7375
}
7476

@@ -108,11 +110,26 @@ function convertParameters(
108110
declaration
109111
);
110112

113+
let type: ts.Type | ts.TypeNode;
114+
if (
115+
declaration &&
116+
ts.isParameter(declaration) &&
117+
ts.isFunctionDeclaration(declaration.parent) &&
118+
declaration.type
119+
) {
120+
type = declaration.type;
121+
} else if (declaration) {
122+
type = context.checker.getTypeOfSymbolAtLocation(
123+
param,
124+
declaration
125+
);
126+
} else {
127+
type = param.type;
128+
}
129+
111130
paramRefl.type = context.converter.convertType(
112131
context.withScope(paramRefl),
113-
declaration
114-
? context.checker.getTypeOfSymbolAtLocation(param, declaration)
115-
: param.type
132+
type
116133
);
117134

118135
let isOptional = false;

src/lib/converter/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,17 @@ const referenceConverter: TypeConverter<
579579
> = {
580580
kind: [ts.SyntaxKind.TypeReference],
581581
convert(context, node) {
582+
const isArray =
583+
context.checker.typeToTypeNode(
584+
context.checker.getTypeAtLocation(node.typeName),
585+
void 0,
586+
ts.NodeBuilderFlags.IgnoreErrors
587+
)?.kind === ts.SyntaxKind.ArrayType;
588+
589+
if (isArray) {
590+
return new ArrayType(convertType(context, node.typeArguments?.[0]));
591+
}
592+
582593
const symbol = context.expectSymbolAtLocation(node.typeName);
583594

584595
const name = node.typeName.getText();

src/test/converter/alias/alias.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,11 @@ export namespace GH1330 {
6565
export namespace GH1408 {
6666
export declare function foo<T extends unknown[]>(): T;
6767
}
68+
69+
export namespace GH1454 {
70+
export type Foo = string | number;
71+
export type Bar = string | number;
72+
73+
export declare function bar(x: Bar): Bar;
74+
export declare function foo(x: Foo): Foo;
75+
}

src/test/converter/alias/specs.json

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,143 @@
238238
}
239239
]
240240
},
241+
{
242+
"id": 43,
243+
"name": "GH1454",
244+
"kind": 2,
245+
"kindString": "Namespace",
246+
"flags": {},
247+
"children": [
248+
{
249+
"id": 51,
250+
"name": "Bar",
251+
"kind": 4194304,
252+
"kindString": "Type alias",
253+
"flags": {},
254+
"type": {
255+
"type": "union",
256+
"types": [
257+
{
258+
"type": "intrinsic",
259+
"name": "string"
260+
},
261+
{
262+
"type": "intrinsic",
263+
"name": "number"
264+
}
265+
]
266+
}
267+
},
268+
{
269+
"id": 50,
270+
"name": "Foo",
271+
"kind": 4194304,
272+
"kindString": "Type alias",
273+
"flags": {},
274+
"type": {
275+
"type": "union",
276+
"types": [
277+
{
278+
"type": "intrinsic",
279+
"name": "string"
280+
},
281+
{
282+
"type": "intrinsic",
283+
"name": "number"
284+
}
285+
]
286+
}
287+
},
288+
{
289+
"id": 44,
290+
"name": "bar",
291+
"kind": 64,
292+
"kindString": "Function",
293+
"flags": {},
294+
"signatures": [
295+
{
296+
"id": 45,
297+
"name": "bar",
298+
"kind": 4096,
299+
"kindString": "Call signature",
300+
"flags": {},
301+
"parameters": [
302+
{
303+
"id": 46,
304+
"name": "x",
305+
"kind": 32768,
306+
"kindString": "Parameter",
307+
"flags": {},
308+
"type": {
309+
"type": "reference",
310+
"id": 51,
311+
"name": "Bar"
312+
}
313+
}
314+
],
315+
"type": {
316+
"type": "reference",
317+
"id": 51,
318+
"name": "Bar"
319+
}
320+
}
321+
]
322+
},
323+
{
324+
"id": 47,
325+
"name": "foo",
326+
"kind": 64,
327+
"kindString": "Function",
328+
"flags": {},
329+
"signatures": [
330+
{
331+
"id": 48,
332+
"name": "foo",
333+
"kind": 4096,
334+
"kindString": "Call signature",
335+
"flags": {},
336+
"parameters": [
337+
{
338+
"id": 49,
339+
"name": "x",
340+
"kind": 32768,
341+
"kindString": "Parameter",
342+
"flags": {},
343+
"type": {
344+
"type": "reference",
345+
"id": 50,
346+
"name": "Foo"
347+
}
348+
}
349+
],
350+
"type": {
351+
"type": "reference",
352+
"id": 50,
353+
"name": "Foo"
354+
}
355+
}
356+
]
357+
}
358+
],
359+
"groups": [
360+
{
361+
"title": "Type aliases",
362+
"kind": 4194304,
363+
"children": [
364+
51,
365+
50
366+
]
367+
},
368+
{
369+
"title": "Functions",
370+
"kind": 64,
371+
"children": [
372+
44,
373+
47
374+
]
375+
}
376+
]
377+
},
241378
{
242379
"id": 21,
243380
"name": "HorribleRecursiveTypeThatShouldNotBeUsedByAnyone",
@@ -746,7 +883,8 @@
746883
"kind": 2,
747884
"children": [
748885
28,
749-
39
886+
39,
887+
43
750888
]
751889
},
752890
{

src/test/converter/function/specs.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -520,16 +520,16 @@
520520
"type": "union",
521521
"types": [
522522
{
523-
"type": "intrinsic",
524-
"name": "undefined"
523+
"type": "reference",
524+
"name": "T"
525525
},
526526
{
527527
"type": "literal",
528528
"value": null
529529
},
530530
{
531-
"type": "reference",
532-
"name": "T"
531+
"type": "intrinsic",
532+
"name": "undefined"
533533
}
534534
]
535535
}
@@ -1004,16 +1004,16 @@
10041004
"type": "union",
10051005
"types": [
10061006
{
1007-
"type": "intrinsic",
1008-
"name": "undefined"
1007+
"type": "reference",
1008+
"name": "T"
10091009
},
10101010
{
10111011
"type": "literal",
10121012
"value": null
10131013
},
10141014
{
1015-
"type": "reference",
1016-
"name": "T"
1015+
"type": "intrinsic",
1016+
"name": "undefined"
10171017
}
10181018
]
10191019
}

0 commit comments

Comments
 (0)