diff --git a/src/lib/converter/types/alias.ts b/src/lib/converter/types/alias.ts index ccd726c9a..45409c47c 100644 --- a/src/lib/converter/types/alias.ts +++ b/src/lib/converter/types/alias.ts @@ -1,11 +1,11 @@ import * as ts from 'typescript'; import { ReferenceType } from '../../models/index'; -import { Component, ConverterTypeComponent, TypeNodeConverter } from '../components'; +import { Component, ConverterTypeComponent, TypeConverter } from '../components'; import { Context } from '../context'; @Component({name: 'type:alias'}) -export class AliasConverter extends ConverterTypeComponent implements TypeNodeConverter { +export class AliasConverter extends ConverterTypeComponent implements TypeConverter { /** * The priority this converter should be executed with. * A higher priority means the converter will be applied earlier. @@ -78,4 +78,18 @@ export class AliasConverter extends ConverterTypeComponent implements TypeNodeCo return result; } + + supportsType(context: Context, type: ts.Type): boolean { + return Boolean(type.aliasSymbol) + } + + convertType(context: Context, type: ts.Type & { aliasSymbol: ts.Symbol }): ReferenceType { + const result = new ReferenceType(type.aliasSymbol.name, ReferenceType.SYMBOL_FQN_RESOLVE_BY_NAME); + + if (type.aliasTypeArguments) { + result.typeArguments = this.owner.convertTypes(context, undefined, type.aliasTypeArguments); + } + + return result; + } } diff --git a/src/test/converter/alias/alias.ts b/src/test/converter/alias/alias.ts index 6de9b3791..7a88ff255 100644 --- a/src/test/converter/alias/alias.ts +++ b/src/test/converter/alias/alias.ts @@ -33,3 +33,20 @@ export type HorribleRecursiveTypeThatShouldNotBeUsedByAnyone }[T['length'] extends 0 ? 0 : 1]; + +namespace GH1330 { + type ExampleParam = Example + interface Example {} + + declare const makeExample: () => Example + declare const makeExample2: () => ExampleParam + + // Recursive type when we don't have a type node. + export const testValue = makeExample() + export const testValue2 = makeExample2() + + type HasProp = { key: T } + + declare const makeProp: (x: T) => HasProp + export const testValue3 = makeProp(1) +} diff --git a/src/test/converter/alias/specs.json b/src/test/converter/alias/specs.json index d2a6fc0b3..3e60ff3d8 100644 --- a/src/test/converter/alias/specs.json +++ b/src/test/converter/alias/specs.json @@ -14,6 +14,313 @@ }, "originalName": "%BASE%/alias/alias.ts", "children": [ + { + "id": 29, + "name": "GH1330", + "kind": 2, + "kindString": "Namespace", + "flags": {}, + "children": [ + { + "id": 30, + "name": "Example", + "kind": 256, + "kindString": "Interface", + "flags": {}, + "typeParameter": [ + { + "id": 31, + "name": "T", + "kind": 131072, + "kindString": "Type parameter", + "flags": {}, + "type": { + "type": "reference", + "id": 32, + "name": "ExampleParam" + } + } + ] + }, + { + "id": 32, + "name": "ExampleParam", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "type": { + "type": "reference", + "id": 30, + "name": "Example" + } + }, + { + "id": 41, + "name": "HasProp", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "typeParameter": [ + { + "id": 42, + "name": "T", + "kind": 131072, + "kindString": "Type parameter", + "flags": {} + } + ], + "type": { + "type": "reflection", + "declaration": { + "id": 43, + "name": "__type", + "kind": 65536, + "kindString": "Type literal", + "flags": {}, + "children": [ + { + "id": 44, + "name": "key", + "kind": 32, + "kindString": "Variable", + "flags": {}, + "type": { + "type": "typeParameter", + "name": "T" + } + } + ], + "groups": [ + { + "title": "Variables", + "kind": 32, + "children": [ + 44 + ] + } + ] + } + } + }, + { + "id": 33, + "name": "makeExample", + "kind": 32, + "kindString": "Variable", + "flags": { + "isConst": true + }, + "type": { + "type": "reflection", + "declaration": { + "id": 34, + "name": "__type", + "kind": 65536, + "kindString": "Type literal", + "flags": {}, + "signatures": [ + { + "id": 35, + "name": "__call", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "type": { + "type": "reference", + "id": 30, + "name": "Example" + } + } + ] + } + } + }, + { + "id": 36, + "name": "makeExample2", + "kind": 32, + "kindString": "Variable", + "flags": { + "isConst": true + }, + "type": { + "type": "reflection", + "declaration": { + "id": 37, + "name": "__type", + "kind": 65536, + "kindString": "Type literal", + "flags": {}, + "signatures": [ + { + "id": 38, + "name": "__call", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "type": { + "type": "reference", + "id": 32, + "name": "ExampleParam" + } + } + ] + } + } + }, + { + "id": 45, + "name": "makeProp", + "kind": 32, + "kindString": "Variable", + "flags": { + "isConst": true + }, + "type": { + "type": "reflection", + "declaration": { + "id": 46, + "name": "__type", + "kind": 65536, + "kindString": "Type literal", + "flags": {}, + "signatures": [ + { + "id": 47, + "name": "__call", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "typeParameter": [ + { + "id": 48, + "name": "T", + "kind": 131072, + "kindString": "Type parameter", + "flags": {} + } + ], + "parameters": [ + { + "id": 49, + "name": "x", + "kind": 32768, + "kindString": "Parameter", + "flags": {}, + "type": { + "type": "typeParameter", + "name": "T" + } + } + ], + "type": { + "type": "reference", + "id": 41, + "typeArguments": [ + { + "type": "typeParameter", + "name": "T" + } + ], + "name": "HasProp" + } + } + ] + } + } + }, + { + "id": 39, + "name": "testValue", + "kind": 32, + "kindString": "Variable", + "flags": { + "isExported": true, + "isConst": true + }, + "type": { + "type": "reference", + "id": 30, + "typeArguments": [ + { + "type": "reference", + "id": 32, + "name": "ExampleParam" + } + ], + "name": "Example" + }, + "defaultValue": "makeExample()" + }, + { + "id": 40, + "name": "testValue2", + "kind": 32, + "kindString": "Variable", + "flags": { + "isExported": true, + "isConst": true + }, + "type": { + "type": "reference", + "id": 32, + "name": "ExampleParam" + }, + "defaultValue": "makeExample2()" + }, + { + "id": 50, + "name": "testValue3", + "kind": 32, + "kindString": "Variable", + "flags": { + "isExported": true, + "isConst": true + }, + "type": { + "type": "reference", + "id": 41, + "typeArguments": [ + { + "type": "intrinsic", + "name": "number" + } + ], + "name": "HasProp" + }, + "defaultValue": "makeProp(1)" + } + ], + "groups": [ + { + "title": "Interfaces", + "kind": 256, + "children": [ + 30 + ] + }, + { + "title": "Type aliases", + "kind": 4194304, + "children": [ + 32, + 41 + ] + }, + { + "title": "Variables", + "kind": 32, + "children": [ + 33, + 36, + 45, + 39, + 40, + 50 + ] + } + ] + }, { "id": 22, "name": "HorribleRecursiveTypeThatShouldNotBeUsedByAnyone", @@ -514,6 +821,13 @@ } ], "groups": [ + { + "title": "Namespaces", + "kind": 2, + "children": [ + 29 + ] + }, { "title": "Type aliases", "kind": 4194304,