diff --git a/packages/babel-generator/src/generators/typescript.ts b/packages/babel-generator/src/generators/typescript.ts index 34bfc5b914e3..cbb716f6ac4f 100644 --- a/packages/babel-generator/src/generators/typescript.ts +++ b/packages/babel-generator/src/generators/typescript.ts @@ -414,7 +414,7 @@ export function TSIndexedAccessType( } export function TSMappedType(this: Printer, node: t.TSMappedType) { - const { nameType, optional, readonly, typeParameter } = node; + const { nameType, optional, readonly, typeParameter, typeAnnotation } = node; this.token("{"); this.space(); if (readonly) { @@ -443,13 +443,15 @@ 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); } - this.token(":"); - this.space(); - this.print(node.typeAnnotation, node); this.space(); this.token("}"); } diff --git a/packages/babel-generator/test/fixtures/typescript/mapped-type/input.ts b/packages/babel-generator/test/fixtures/typescript/mapped-type/input.ts new file mode 100644 index 000000000000..9c446e4ab103 --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/mapped-type/input.ts @@ -0,0 +1 @@ +type Bar = { [key in Foo] }; diff --git a/packages/babel-generator/test/fixtures/typescript/mapped-type/output.js b/packages/babel-generator/test/fixtures/typescript/mapped-type/output.js new file mode 100644 index 000000000000..ebccaaab98fc --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/mapped-type/output.js @@ -0,0 +1 @@ +type Bar = { [key in Foo] }; \ No newline at end of file