diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index d2f3f334c1ff..ecf1255af952 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -2526,7 +2526,7 @@ export default (superClass: Class): Class => // Allow type annotations inside of a parameter list. parseAssignableListItemTypes(param: N.Pattern) { if (this.eat(tt.question)) { - if (param.type !== "Identifier") { + if (param.type !== "Identifier" && !this.state.isDeclareContext) { this.raise(param.start, TSErrors.PatternIsOptional); } diff --git a/packages/babel-parser/test/fixtures/typescript/function/declare-pattern-parameters/input.ts b/packages/babel-parser/test/fixtures/typescript/function/declare-pattern-parameters/input.ts new file mode 100644 index 000000000000..f74f3fd7ca2a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/function/declare-pattern-parameters/input.ts @@ -0,0 +1 @@ +declare function f([]?, {}) diff --git a/packages/babel-parser/test/fixtures/typescript/function/declare-pattern-parameters/output.json b/packages/babel-parser/test/fixtures/typescript/function/declare-pattern-parameters/output.json new file mode 100644 index 000000000000..4e7f0f35e396 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/function/declare-pattern-parameters/output.json @@ -0,0 +1,38 @@ +{ + "type": "File", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, + "program": { + "type": "Program", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSDeclareFunction", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, + "declare": true, + "id": { + "type": "Identifier", + "start":17,"end":18,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":18},"identifierName":"f"}, + "name": "f" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "ArrayPattern", + "start":19,"end":22,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":22}}, + "elements": [], + "optional": true + }, + { + "type": "ObjectPattern", + "start":24,"end":26,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":26}}, + "properties": [] + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file