Typescript - Tuple elements can be optional (#8720)

This commit is contained in:
Retsam
2018-10-02 12:29:51 -04:00
committed by Brian Ng
parent 3c87401714
commit a5b5ed928d
21 changed files with 584 additions and 1 deletions

View File

@@ -503,13 +503,23 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const node: N.TsTupleType = this.startNode();
node.elementTypes = this.tsParseBracketedList(
"TupleElementTypes",
this.tsParseType.bind(this),
this.tsParseTupleElementType.bind(this),
/* bracket */ true,
/* skipFirstToken */ false,
);
return this.finishNode(node, "TSTupleType");
}
tsParseTupleElementType(): N.TsType {
const type = this.tsParseType();
if (this.eat(tt.question)) {
const optionalTypeNode: N.TsOptionalType = this.startNodeAtNode(type);
optionalTypeNode.typeAnnotation = type;
return this.finishNode(optionalTypeNode, "TSOptionalType");
}
return type;
}
tsParseParenthesizedType(): N.TsParenthesizedType {
const node = this.startNode();
this.expect(tt.parenL);

View File

@@ -1104,6 +1104,7 @@ export type TsType =
| TsTypeLiteral
| TsArrayType
| TsTupleType
| TsOptionalType
| TsUnionOrIntersectionType
| TsConditionalType
| TsInferType
@@ -1184,6 +1185,11 @@ export type TsTupleType = TsTypeBase & {
elementTypes: $ReadOnlyArray<TsType>,
};
export type TsOptionalType = TsTypeBase & {
type: "TSOptionalType",
typeAnnotation: TsType,
};
export type TsUnionOrIntersectionType = TsUnionType | TsIntersectionType;
export type TsUnionOrIntersectionTypeBase = TsTypeBase & {

View File

@@ -0,0 +1 @@
let x: [];

View File

@@ -0,0 +1,117 @@
{
"type": "File",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"program": {
"type": "Program",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 9
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "x"
},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 5,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 9
}
},
"typeAnnotation": {
"type": "TSTupleType",
"start": 7,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 9
}
},
"elementTypes": []
}
}
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
let x: [string, number?, (string | number)?]

View File

@@ -0,0 +1,240 @@
{
"type": "File",
"start": 0,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 44
}
},
"program": {
"type": "Program",
"start": 0,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 44
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 44
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 44
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 44
},
"identifierName": "x"
},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 5,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 44
}
},
"typeAnnotation": {
"type": "TSTupleType",
"start": 7,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 44
}
},
"elementTypes": [
{
"type": "TSStringKeyword",
"start": 8,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 14
}
}
},
{
"type": "TSOptionalType",
"start": 16,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 23
}
},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start": 16,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 22
}
}
}
},
{
"type": "TSOptionalType",
"start": 25,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 43
}
},
"typeAnnotation": {
"type": "TSParenthesizedType",
"start": 25,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 42
}
},
"typeAnnotation": {
"type": "TSUnionType",
"start": 26,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 41
}
},
"types": [
{
"type": "TSStringKeyword",
"start": 26,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 32
}
}
},
{
"type": "TSNumberKeyword",
"start": 35,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 35
},
"end": {
"line": 1,
"column": 41
}
}
}
]
}
}
}
]
}
}
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
let x: [number, number, number];

View File

@@ -0,0 +1,163 @@
{
"type": "File",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 32
}
},
"program": {
"type": "Program",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 32
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 32
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 31
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 31
},
"identifierName": "x"
},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 5,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 31
}
},
"typeAnnotation": {
"type": "TSTupleType",
"start": 7,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 31
}
},
"elementTypes": [
{
"type": "TSNumberKeyword",
"start": 8,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 14
}
}
},
{
"type": "TSNumberKeyword",
"start": 16,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 22
}
}
},
{
"type": "TSNumberKeyword",
"start": 24,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 24
},
"end": {
"line": 1,
"column": 30
}
}
}
]
}
}
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
}
}