1
1
import * as ts from 'typescript' ;
2
2
3
- import { Type , UnionType } from '../../models/types/index' ;
3
+ import { Type , UnionType , IntersectionType } from '../../models/types/index' ;
4
4
import { Component , ConverterTypeComponent , TypeConverter } from '../components' ;
5
5
import { Context } from '../context' ;
6
6
7
- @Component ( { name : 'type:union' } )
8
- export class UnionConverter extends ConverterTypeComponent implements TypeConverter < ts . UnionType , ts . UnionTypeNode > {
7
+ @Component ( { name : 'type:union-or-intersection ' } )
8
+ export class UnionOrIntersectionConverter extends ConverterTypeComponent implements TypeConverter < ts . UnionOrIntersectionType , ts . UnionOrIntersectionTypeNode > {
9
9
/**
10
10
* Test whether this converter can handle the given TypeScript node.
11
11
*/
12
- supportsNode ( context : Context , node : ts . UnionTypeNode ) : boolean {
13
- return node . kind === ts . SyntaxKind . UnionType ;
12
+ supportsNode ( context : Context , node : ts . UnionOrIntersectionTypeNode ) : boolean {
13
+ return node . kind === ts . SyntaxKind . UnionType || node . kind === ts . SyntaxKind . IntersectionType ;
14
14
}
15
15
16
16
/**
17
17
* Test whether this converter can handle the given TypeScript type.
18
18
*/
19
- supportsType ( context : Context , type : ts . UnionType ) : boolean {
20
- return ! ! ( type . flags & ts . TypeFlags . Union ) ;
19
+ supportsType ( context : Context , type : ts . UnionOrIntersectionType ) : boolean {
20
+ return ! ! ( type . flags & ts . TypeFlags . UnionOrIntersection ) ;
21
21
}
22
22
23
23
/**
24
24
* Convert the given union type node to its type reflection.
25
25
*
26
- * This is a node based converter, see [[convertUnionType ]] for the type equivalent.
26
+ * This is a node based converter, see [[convertType ]] for the type equivalent.
27
27
*
28
28
* ```
29
29
* let someValue: string|number;
30
30
* ```
31
31
*
32
32
* @param context The context object describing the current state the converter is in.
33
- * @param node The union type node that should be converted.
33
+ * @param node The union or intersection type node that should be converted.
34
34
* @returns The type reflection representing the given union type node.
35
35
*/
36
- convertNode ( context : Context , node : ts . UnionTypeNode ) : UnionType {
36
+ convertNode ( context : Context , node : ts . UnionOrIntersectionTypeNode ) : UnionType | IntersectionType {
37
37
let types : Type [ ] = [ ] ;
38
38
if ( node . types ) {
39
39
types = node . types . map ( ( n ) => this . owner . convertType ( context , n ) ) ;
40
40
} else {
41
41
types = [ ] ;
42
42
}
43
43
44
- return new UnionType ( types ) ;
44
+ return node . kind === ts . SyntaxKind . IntersectionType ? new IntersectionType ( types ) : new UnionType ( types ) ;
45
45
}
46
46
47
47
/**
@@ -57,14 +57,14 @@ export class UnionConverter extends ConverterTypeComponent implements TypeConver
57
57
* @param type The union type that should be converted.
58
58
* @returns The type reflection representing the given union type.
59
59
*/
60
- convertType ( context : Context , type : ts . UnionType ) : UnionType {
60
+ convertType ( context : Context , type : ts . UnionOrIntersectionType ) : UnionType | IntersectionType {
61
61
let types : Type [ ] ;
62
62
if ( type && type . types ) {
63
63
types = type . types . map ( ( t ) => this . owner . convertType ( context , null , t ) ) ;
64
64
} else {
65
65
types = [ ] ;
66
66
}
67
67
68
- return new UnionType ( types ) ;
68
+ return ! ! ( type . flags & ts . TypeFlags . Intersection ) ? new IntersectionType ( types ) : new UnionType ( types ) ;
69
69
}
70
70
}
0 commit comments