diff --git a/src/lib/converter/factories/signature.ts b/src/lib/converter/factories/signature.ts index 82233f876..c94275c3e 100644 --- a/src/lib/converter/factories/signature.ts +++ b/src/lib/converter/factories/signature.ts @@ -68,7 +68,9 @@ export function createSignature( } else { sigRef.type = context.converter.convertType( context.withScope(sigRef), - signature.getReturnType() + (declaration?.kind === ts.SyntaxKind.FunctionDeclaration && + declaration.type) || + signature.getReturnType() ); } @@ -108,11 +110,26 @@ function convertParameters( declaration ); + let type: ts.Type | ts.TypeNode; + if ( + declaration && + ts.isParameter(declaration) && + ts.isFunctionDeclaration(declaration.parent) && + declaration.type + ) { + type = declaration.type; + } else if (declaration) { + type = context.checker.getTypeOfSymbolAtLocation( + param, + declaration + ); + } else { + type = param.type; + } + paramRefl.type = context.converter.convertType( context.withScope(paramRefl), - declaration - ? context.checker.getTypeOfSymbolAtLocation(param, declaration) - : param.type + type ); let isOptional = false; diff --git a/src/lib/converter/types.ts b/src/lib/converter/types.ts index 23a8170c8..4ade28055 100644 --- a/src/lib/converter/types.ts +++ b/src/lib/converter/types.ts @@ -579,6 +579,17 @@ const referenceConverter: TypeConverter< > = { kind: [ts.SyntaxKind.TypeReference], convert(context, node) { + const isArray = + context.checker.typeToTypeNode( + context.checker.getTypeAtLocation(node.typeName), + void 0, + ts.NodeBuilderFlags.IgnoreErrors + )?.kind === ts.SyntaxKind.ArrayType; + + if (isArray) { + return new ArrayType(convertType(context, node.typeArguments?.[0])); + } + const symbol = context.expectSymbolAtLocation(node.typeName); const name = node.typeName.getText(); diff --git a/src/test/converter/alias/alias.ts b/src/test/converter/alias/alias.ts index 2ea883479..17197dab9 100644 --- a/src/test/converter/alias/alias.ts +++ b/src/test/converter/alias/alias.ts @@ -65,3 +65,11 @@ export namespace GH1330 { export namespace GH1408 { export declare function foo(): T; } + +export namespace GH1454 { + export type Foo = string | number; + export type Bar = string | number; + + export declare function bar(x: Bar): Bar; + export declare function foo(x: Foo): Foo; +} diff --git a/src/test/converter/alias/specs.json b/src/test/converter/alias/specs.json index 9d60e0d99..8c075064c 100644 --- a/src/test/converter/alias/specs.json +++ b/src/test/converter/alias/specs.json @@ -238,6 +238,143 @@ } ] }, + { + "id": 43, + "name": "GH1454", + "kind": 2, + "kindString": "Namespace", + "flags": {}, + "children": [ + { + "id": 51, + "name": "Bar", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "type": { + "type": "union", + "types": [ + { + "type": "intrinsic", + "name": "string" + }, + { + "type": "intrinsic", + "name": "number" + } + ] + } + }, + { + "id": 50, + "name": "Foo", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "type": { + "type": "union", + "types": [ + { + "type": "intrinsic", + "name": "string" + }, + { + "type": "intrinsic", + "name": "number" + } + ] + } + }, + { + "id": 44, + "name": "bar", + "kind": 64, + "kindString": "Function", + "flags": {}, + "signatures": [ + { + "id": 45, + "name": "bar", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "parameters": [ + { + "id": 46, + "name": "x", + "kind": 32768, + "kindString": "Parameter", + "flags": {}, + "type": { + "type": "reference", + "id": 51, + "name": "Bar" + } + } + ], + "type": { + "type": "reference", + "id": 51, + "name": "Bar" + } + } + ] + }, + { + "id": 47, + "name": "foo", + "kind": 64, + "kindString": "Function", + "flags": {}, + "signatures": [ + { + "id": 48, + "name": "foo", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "parameters": [ + { + "id": 49, + "name": "x", + "kind": 32768, + "kindString": "Parameter", + "flags": {}, + "type": { + "type": "reference", + "id": 50, + "name": "Foo" + } + } + ], + "type": { + "type": "reference", + "id": 50, + "name": "Foo" + } + } + ] + } + ], + "groups": [ + { + "title": "Type aliases", + "kind": 4194304, + "children": [ + 51, + 50 + ] + }, + { + "title": "Functions", + "kind": 64, + "children": [ + 44, + 47 + ] + } + ] + }, { "id": 21, "name": "HorribleRecursiveTypeThatShouldNotBeUsedByAnyone", @@ -746,7 +883,8 @@ "kind": 2, "children": [ 28, - 39 + 39, + 43 ] }, { diff --git a/src/test/converter/function/specs.json b/src/test/converter/function/specs.json index 8d3578909..ad639e962 100644 --- a/src/test/converter/function/specs.json +++ b/src/test/converter/function/specs.json @@ -520,16 +520,16 @@ "type": "union", "types": [ { - "type": "intrinsic", - "name": "undefined" + "type": "reference", + "name": "T" }, { "type": "literal", "value": null }, { - "type": "reference", - "name": "T" + "type": "intrinsic", + "name": "undefined" } ] } @@ -1004,16 +1004,16 @@ "type": "union", "types": [ { - "type": "intrinsic", - "name": "undefined" + "type": "reference", + "name": "T" }, { "type": "literal", "value": null }, { - "type": "reference", - "name": "T" + "type": "intrinsic", + "name": "undefined" } ] }