diff --git a/packages/babel-generator/src/generators/typescript.ts b/packages/babel-generator/src/generators/typescript.ts index cbb716f6ac4f..aae7f979d016 100644 --- a/packages/babel-generator/src/generators/typescript.ts +++ b/packages/babel-generator/src/generators/typescript.ts @@ -443,11 +443,12 @@ export function TSMappedType(this: Printer, node: t.TSMappedType) { this.token("]"); + if (optional) { + tokenIfPlusMinus(this, optional); + this.token("?"); + } + if (typeAnnotation) { - if (optional) { - tokenIfPlusMinus(this, optional); - this.token("?"); - } this.token(":"); this.space(); this.print(typeAnnotation, node); diff --git a/packages/babel-generator/test/fixtures/typescript/mapped-type/input.ts b/packages/babel-generator/test/fixtures/typescript/mapped-type/input.ts index 9c446e4ab103..611dd15d3377 100644 --- a/packages/babel-generator/test/fixtures/typescript/mapped-type/input.ts +++ b/packages/babel-generator/test/fixtures/typescript/mapped-type/input.ts @@ -1 +1,4 @@ type Bar = { [key in Foo] }; +type Foo2 = { [key in Bar]? }; +type Foo3 = { [key in keyof Bar]-? }; +type Foo4 = { [key in keyof Bar]+? }; diff --git a/packages/babel-generator/test/fixtures/typescript/mapped-type/output.js b/packages/babel-generator/test/fixtures/typescript/mapped-type/output.js index ebccaaab98fc..37b392920270 100644 --- a/packages/babel-generator/test/fixtures/typescript/mapped-type/output.js +++ b/packages/babel-generator/test/fixtures/typescript/mapped-type/output.js @@ -1 +1,4 @@ -type Bar = { [key in Foo] }; \ No newline at end of file +type Bar = { [key in Foo] }; +type Foo2 = { [key in Bar]? }; +type Foo3 = { [key in keyof Bar]-? }; +type Foo4 = { [key in keyof Bar]+? }; \ No newline at end of file