-
-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
261 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as ts from 'typescript'; | ||
|
||
import { ConditionalType } from '../../models/types'; | ||
import { Component, ConverterTypeComponent, TypeConverter } from '../components'; | ||
import { Context } from '../context'; | ||
|
||
@Component({name: 'type:conditional'}) | ||
export class ConditionalConverter extends ConverterTypeComponent implements TypeConverter<ts.ConditionalType, ts.ConditionalTypeNode> { | ||
/** | ||
* Test whether this converter can handle the given TypeScript node. | ||
*/ | ||
supportsNode(context: Context, node: ts.ConditionalTypeNode): boolean { | ||
return node.kind === ts.SyntaxKind.ConditionalType; | ||
} | ||
|
||
/** | ||
* Test whether this converter can handle the given TypeScript type. | ||
*/ | ||
supportsType(context: Context, type: ts.ConditionalType): boolean { | ||
return !!(type.flags & ts.TypeFlags.Conditional); | ||
} | ||
|
||
/** | ||
* Convert the given conditional type node to its type reflection. | ||
* | ||
* This is a node based converter, see [[convertType]] for the type equivalent. | ||
* | ||
* @param context The context object describing the current state the converter is in. | ||
* @param node The conditional or intersection type node that should be converted. | ||
* @returns The type reflection representing the given conditional type node. | ||
*/ | ||
convertNode(context: Context, node: ts.ConditionalTypeNode): ConditionalType { | ||
return new ConditionalType( | ||
this.owner.convertType(context, node.checkType), | ||
this.owner.convertType(context, node.extendsType), | ||
this.owner.convertType(context, node.trueType), | ||
this.owner.convertType(context, node.falseType) | ||
); | ||
} | ||
|
||
/** | ||
* Convert the given conditional type to its type reflection. | ||
* | ||
* This is a type based converter, see [[convertNode]] for the node equivalent. | ||
* | ||
* @param context The context object describing the current state the converter is in. | ||
* @param type The conditional type that should be converted. | ||
* @returns The type reflection representing the given conditional type. | ||
*/ | ||
convertType(context: Context, type: ts.ConditionalType): ConditionalType { | ||
return new ConditionalType( | ||
this.owner.convertType(context, null, type.checkType), | ||
this.owner.convertType(context, null, type.extendsType), | ||
this.owner.convertType(context, null, type.resolvedTrueType), | ||
this.owner.convertType(context, null, type.resolvedFalseType) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Type } from './abstract'; | ||
|
||
/** | ||
* Represents a conditional type. | ||
* | ||
* ~~~ | ||
* let value: C extends E ? T : F; | ||
* let value2: Check extends Extends ? True : False; | ||
* ~~~ | ||
*/ | ||
export class ConditionalType extends Type { | ||
/** | ||
* The type name identifier. | ||
*/ | ||
readonly type: string = 'conditional'; | ||
|
||
constructor( | ||
public checkType: Type, | ||
public extendsType: Type, | ||
public trueType: Type, | ||
public falseType: Type | ||
) { | ||
super(); | ||
} | ||
|
||
/** | ||
* Clone this type. | ||
* | ||
* @return A clone of this type. | ||
*/ | ||
clone(): Type { | ||
return new ConditionalType(this.checkType, this.extendsType, this.trueType, this.falseType); | ||
} | ||
|
||
/** | ||
* Test whether this type equals the given type. | ||
* | ||
* @param type The type that should be checked for equality. | ||
* @returns TRUE if the given type equals this type, FALSE otherwise. | ||
*/ | ||
equals(type: any): boolean { | ||
if (!(type instanceof ConditionalType)) { | ||
return false; | ||
} | ||
return this.checkType.equals(type.checkType) && | ||
this.extendsType.equals(type.extendsType) && | ||
this.trueType.equals(type.trueType) && | ||
this.falseType.equals(type.falseType); | ||
} | ||
|
||
/** | ||
* Return a raw object representation of this type. | ||
* @deprecated Use serializers instead | ||
*/ | ||
toObject(): any { | ||
const result: any = super.toObject(); | ||
|
||
result.checkType = this.checkType.toObject(); | ||
result.extendsType = this.extendsType.toObject(); | ||
result.trueType = this.trueType.toObject(); | ||
result.falseType = this.falseType.toObject(); | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Return a string representation of this type. | ||
*/ | ||
toString() { | ||
return this.checkType + ' extends ' + this.extendsType + ' ? ' + this.trueType + ' : ' + this.falseType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Component } from '../../../utils/component'; | ||
import { ConditionalType } from '../../../models'; | ||
import { TypeSerializerComponent } from '../../components'; | ||
|
||
@Component({name: 'serializer:conditional-type'}) | ||
export class ConditionalTypeSerializer extends TypeSerializerComponent<ConditionalType> { | ||
|
||
initialize(): void { | ||
super.initialize(); | ||
this.supports = (t: ConditionalType) => t instanceof ConditionalType; | ||
} | ||
|
||
toObject(conditional: ConditionalType, obj?: any): any { | ||
obj = obj || {}; | ||
|
||
obj.checkType = this.owner.toObject(conditional.checkType); | ||
obj.extendsType = this.owner.toObject(conditional.extendsType); | ||
obj.trueType = this.owner.toObject(conditional.trueType); | ||
obj.falseType = this.owner.toObject(conditional.falseType); | ||
|
||
return obj; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
function A<T> (x: T): T extends number ? number : string { | ||
return 'number' === typeof x ? x : x.toString() as any; | ||
class C { | ||
foo: number; | ||
} | ||
|
||
function A<T> (x: T): T extends number ? number : C { | ||
return 'number' === typeof x ? x : new C() as any; | ||
} | ||
|
||
type B<T> = T extends string ? 'string' : 'notstring'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters