11import * as ts from 'typescript' ;
22
3- import { Type , IntrinsicType } from '../../models/index' ;
4- import { Component , ConverterTypeComponent , TypeNodeConverter } from '../components' ;
3+ import { Type , ArrayType } from '../../models/index' ;
4+ import { Component , ConverterTypeComponent , TypeConverter } from '../components' ;
55import { Context } from '../context' ;
66
77@Component ( { name : 'type:array' } )
8- export class ArrayConverter extends ConverterTypeComponent implements TypeNodeConverter < ts . Type , ts . ArrayTypeNode > {
8+ export class ArrayConverter extends ConverterTypeComponent implements TypeConverter < ts . TypeReference , ts . ArrayTypeNode > {
99 /**
1010 * Test whether this converter can handle the given TypeScript node.
1111 */
1212 supportsNode ( context : Context , node : ts . ArrayTypeNode ) : boolean {
1313 return node . kind === ts . SyntaxKind . ArrayType ;
1414 }
1515
16+ /**
17+ * Test whether this converter can handle the given TypeScript type.
18+ */
19+ supportsType ( context : Context , type : ts . TypeReference ) : boolean {
20+ // Is there a better way to detect the {"type":"reference","name":"Array","typeArguments":{...}} types that are in fact arrays?
21+ return ! ! ( type . flags & ts . TypeFlags . Object ) && ! ! type . symbol && type . symbol . name === 'Array' && ! type . symbol . parent && ! ! type . typeArguments && type . typeArguments . length === 1 ;
22+ }
23+
1624 /**
1725 * Convert the given array type node to its type reflection.
1826 *
@@ -27,14 +35,28 @@ export class ArrayConverter extends ConverterTypeComponent implements TypeNodeCo
2735 * @returns The type reflection representing the given array type node.
2836 */
2937 convertNode ( context : Context , node : ts . ArrayTypeNode ) : Type {
30- let result = this . owner . convertType ( context , node . elementType ) ;
38+ const result = this . owner . convertType ( context , node . elementType ) ;
3139
32- if ( result ) {
33- result . isArray = true ;
34- } else {
35- result = new IntrinsicType ( 'Array' ) ;
36- }
40+ return new ArrayType ( result ) ;
41+ }
42+
43+ /**
44+ * Convert the given type reference to its type reflection.
45+ *
46+ * This is a type based converter, see [[convertTypeReference]] for the node equivalent.
47+ *
48+ * ```
49+ * class SomeClass { }
50+ * let someValue: SomeClass;
51+ * ```
52+ *
53+ * @param context The context object describing the current state the converter is in.
54+ * @param type The type reference that should be converted.
55+ * @returns The type reflection representing the given type reference.
56+ */
57+ convertType ( context : Context , type : ts . TypeReference ) : Type {
58+ const result = this . owner . convertType ( context , null , type . typeArguments [ 0 ] ) ;
3759
38- return result ;
60+ return new ArrayType ( result ) ;
3961 }
4062}
0 commit comments