TypeScript: Support conditional types syntax (#7404)
Microsoft/TypeScript#21316 and Microsoft/TypeScript#21496
This commit is contained in:
@@ -617,11 +617,22 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return this.finishNode(node, "TSTypeOperator");
|
||||
}
|
||||
|
||||
tsParseInferType(): N.TsInferType {
|
||||
const node = this.startNode();
|
||||
this.expectContextual("infer");
|
||||
const typeParameter = this.startNode();
|
||||
typeParameter.name = this.parseIdentifierName(typeParameter.start);
|
||||
node.typeParameter = this.finishNode(typeParameter, "TypeParameter");
|
||||
return this.finishNode(node, "TSInferType");
|
||||
}
|
||||
|
||||
tsParseTypeOperatorOrHigher(): N.TsType {
|
||||
const operator = ["keyof", "unique"].find(kw => this.isContextual(kw));
|
||||
return operator
|
||||
? this.tsParseTypeOperator(operator)
|
||||
: this.tsParseArrayTypeOrHigher();
|
||||
: this.isContextual("infer")
|
||||
? this.tsParseInferType()
|
||||
: this.tsParseArrayTypeOrHigher();
|
||||
}
|
||||
|
||||
tsParseUnionOrIntersectionType(
|
||||
@@ -774,6 +785,21 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
tsParseType(): N.TsType {
|
||||
// Need to set `state.inType` so that we don't parse JSX in a type context.
|
||||
assert(this.state.inType);
|
||||
const type = this.tsParseNonConditionalType();
|
||||
if (this.hasPrecedingLineBreak() || !this.eat(tt._extends)) {
|
||||
return type;
|
||||
}
|
||||
const node: N.TsConditionalType = this.startNodeAtNode(type);
|
||||
node.checkType = type;
|
||||
node.extendsType = this.tsParseNonConditionalType();
|
||||
this.expect(tt.question);
|
||||
node.trueType = this.tsParseType();
|
||||
this.expect(tt.colon);
|
||||
node.falseType = this.tsParseType();
|
||||
return this.finishNode(node, "TSConditionalType");
|
||||
}
|
||||
|
||||
tsParseNonConditionalType(): N.TsType {
|
||||
if (this.tsIsStartOfFunctionType()) {
|
||||
return this.tsParseFunctionOrConstructorType("TSFunctionType");
|
||||
}
|
||||
|
||||
@@ -1154,6 +1154,19 @@ export type TsIntersectionType = TsUnionOrIntersectionTypeBase & {
|
||||
type: "TSIntersectionType",
|
||||
};
|
||||
|
||||
export type TsConditionalType = TsTypeBase & {
|
||||
type: "TSConditionalType",
|
||||
checkType: TsType,
|
||||
extendsType: TsType,
|
||||
trueType: TsType,
|
||||
falseType: tsType,
|
||||
};
|
||||
|
||||
export type InferType = TsTypeBase & {
|
||||
type: "TSInferType",
|
||||
typeParameter: TypeParameter,
|
||||
};
|
||||
|
||||
export type TsParenthesizedType = TsTypeBase & {
|
||||
type: "TSParenthesizedType",
|
||||
typeAnnotation: TsType,
|
||||
|
||||
Reference in New Issue
Block a user