Skip to content

Commit

Permalink
fix: Improve support for type aliases
Browse files Browse the repository at this point in the history
Fixes #1330
  • Loading branch information
Gerrit0 committed Jul 4, 2020
1 parent f582eb3 commit 7fc721c
Show file tree
Hide file tree
Showing 3 changed files with 347 additions and 2 deletions.
18 changes: 16 additions & 2 deletions 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<ts.Type, ts.TypeReferenceNode> {
export class AliasConverter extends ConverterTypeComponent implements TypeConverter<ts.Type, ts.TypeReferenceNode> {
/**
* The priority this converter should be executed with.
* A higher priority means the converter will be applied earlier.
Expand Down Expand Up @@ -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;
}
}
17 changes: 17 additions & 0 deletions src/test/converter/alias/alias.ts
Expand Up @@ -33,3 +33,20 @@ export type HorribleRecursiveTypeThatShouldNotBeUsedByAnyone<T extends any[], R
[K in keyof R | keyof T[0]]: K extends keyof R ? R[K] : T[0][K]
}>
}[T['length'] extends 0 ? 0 : 1];

namespace GH1330 {
type ExampleParam = Example
interface Example<T extends ExampleParam = ExampleParam> {}

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<T> = { key: T }

declare const makeProp: <T>(x: T) => HasProp<T>
export const testValue3 = makeProp(1)
}
314 changes: 314 additions & 0 deletions src/test/converter/alias/specs.json
Expand Up @@ -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",
Expand Down Expand Up @@ -514,6 +821,13 @@
}
],
"groups": [
{
"title": "Namespaces",
"kind": 2,
"children": [
29
]
},
{
"title": "Type aliases",
"kind": 4194304,
Expand Down

0 comments on commit 7fc721c

Please sign in to comment.