11import * as ts from 'typescript' ;
22
3- import { Type , UnionType } from '../../models/types/index' ;
3+ import { Type , UnionType , IntersectionType } from '../../models/types/index' ;
44import { Component , ConverterTypeComponent , TypeConverter } from '../components' ;
55import { Context } from '../context' ;
66
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 > {
99 /**
1010 * Test whether this converter can handle the given TypeScript node.
1111 */
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 ;
1414 }
1515
1616 /**
1717 * Test whether this converter can handle the given TypeScript type.
1818 */
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 ) ;
2121 }
2222
2323 /**
2424 * Convert the given union type node to its type reflection.
2525 *
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.
2727 *
2828 * ```
2929 * let someValue: string|number;
3030 * ```
3131 *
3232 * @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.
3434 * @returns The type reflection representing the given union type node.
3535 */
36- convertNode ( context : Context , node : ts . UnionTypeNode ) : UnionType {
36+ convertNode ( context : Context , node : ts . UnionOrIntersectionTypeNode ) : UnionType | IntersectionType {
3737 let types : Type [ ] = [ ] ;
3838 if ( node . types ) {
3939 types = node . types . map ( ( n ) => this . owner . convertType ( context , n ) ) ;
4040 } else {
4141 types = [ ] ;
4242 }
4343
44- return new UnionType ( types ) ;
44+ return node . kind === ts . SyntaxKind . IntersectionType ? new IntersectionType ( types ) : new UnionType ( types ) ;
4545 }
4646
4747 /**
@@ -57,14 +57,14 @@ export class UnionConverter extends ConverterTypeComponent implements TypeConver
5757 * @param type The union type that should be converted.
5858 * @returns The type reflection representing the given union type.
5959 */
60- convertType ( context : Context , type : ts . UnionType ) : UnionType {
60+ convertType ( context : Context , type : ts . UnionOrIntersectionType ) : UnionType | IntersectionType {
6161 let types : Type [ ] ;
6262 if ( type && type . types ) {
6363 types = type . types . map ( ( t ) => this . owner . convertType ( context , null , t ) ) ;
6464 } else {
6565 types = [ ] ;
6666 }
6767
68- return new UnionType ( types ) ;
68+ return ! ! ( type . flags & ts . TypeFlags . Intersection ) ? new IntersectionType ( types ) : new UnionType ( types ) ;
6969 }
7070}
0 commit comments