TypeScript 4.0: Allow spread in the middle of tuples (#11753)

This commit is contained in:
Nicolò Ribaudo 2020-06-30 01:04:39 +02:00 committed by Huáng Jùnliàng
parent d7347fb8bd
commit 5b4b3a3e4a
6 changed files with 69 additions and 15 deletions

View File

@ -630,9 +630,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
/* skipFirstToken */ false,
);
// Validate the elementTypes to ensure:
// No mandatory elements may follow optional elements
// If there's a rest element, it must be at the end of the tuple
// Validate the elementTypes to ensure that no mandatory elements
// follow optional elements
let seenOptionalElement = false;
node.elementTypes.forEach(elementNode => {
if (elementNode.type === "TSOptionalType") {
@ -651,12 +650,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const restNode: N.TsRestType = this.startNode();
this.next(); // skips ellipsis
restNode.typeAnnotation = this.tsParseType();
if (
this.match(tt.comma) &&
this.lookaheadCharCode() !== charCodes.rightSquareBracket
) {
this.raiseRestNotLast(this.state.start);
}
return this.finishNode(restNode, "TSRestType");
}

View File

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

View File

@ -1,5 +0,0 @@
{
"sourceType": "module",
"plugins": ["typescript"],
"throws": "Rest element must be last element (1:19)"
}

View File

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

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript"]
}

View File

@ -0,0 +1,62 @@
{
"type": "File",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
"program": {
"type": "Program",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":36,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":36}},
"id": {
"type": "Identifier",
"start":4,"end":36,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":36},"identifierName":"x"},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":5,"end":36,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":36}},
"typeAnnotation": {
"type": "TSTupleType",
"start":7,"end":36,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":36}},
"elementTypes": [
{
"type": "TSRestType",
"start":8,"end":27,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":27}},
"typeAnnotation": {
"type": "TSTupleType",
"start":11,"end":27,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":27}},
"elementTypes": [
{
"type": "TSNumberKeyword",
"start":12,"end":18,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":18}}
},
{
"type": "TSStringKeyword",
"start":20,"end":26,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":26}}
}
]
}
},
{
"type": "TSStringKeyword",
"start":29,"end":35,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":35}}
}
]
}
}
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
}
}