From 4343b72fa6fc6fb8d4ec2d437d21f99d71dfadf9 Mon Sep 17 00:00:00 2001 From: Babel Bot <30521560+liuxingbaoyu@users.noreply.github.com> Date: Fri, 29 Mar 2024 15:33:47 +0800 Subject: [PATCH] fix --- .../babel-generator/src/generators/typescript.ts | 16 +++++++++------- .../fixtures/typescript/mapped-type/input.ts | 1 + .../fixtures/typescript/mapped-type/output.js | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/typescript/mapped-type/input.ts create mode 100644 packages/babel-generator/test/fixtures/typescript/mapped-type/output.js 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