diff --git a/packages/babel-plugin-transform-template-literals/src/index.ts b/packages/babel-plugin-transform-template-literals/src/index.ts index edc0f002ef07..f52accb6a0b6 100644 --- a/packages/babel-plugin-transform-template-literals/src/index.ts +++ b/packages/babel-plugin-transform-template-literals/src/index.ts @@ -1,5 +1,6 @@ import { declare } from "@babel/helper-plugin-utils"; import { template, types as t } from "@babel/core"; +import type { NodePath } from "@babel/traverse"; export interface Options { loose?: boolean; @@ -31,8 +32,9 @@ export default declare((api, options: Options) => { * the second member and convert that one, which reflects the spec behavior * of template literals. */ - function buildConcatCallExpressions(items) { + function buildConcatCallExpressions(items: t.Expression[]): t.CallExpression { let avail = true; + // @ts-expect-error items must not be empty return items.reduce(function (left, right) { let canBeInserted = t.isLiteral(right); @@ -104,8 +106,12 @@ export default declare((api, options: Options) => { }, TemplateLiteral(path) { - const nodes = []; - const expressions = path.get("expressions"); + // Skip TemplateLiteral in TSLiteralType + if (path.parent.type === "TSLiteralType") { + return; + } + const nodes: t.Expression[] = []; + const expressions = path.get("expressions") as NodePath[]; let index = 0; for (const elem of path.node.quasis) { diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/ts-literal-type/input.ts b/packages/babel-plugin-transform-template-literals/test/fixtures/default/ts-literal-type/input.ts new file mode 100644 index 000000000000..65e2a36d96eb --- /dev/null +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/ts-literal-type/input.ts @@ -0,0 +1,2 @@ +type World = "world"; +type Greeting = `hello ${World}`; diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/ts-literal-type/options.json b/packages/babel-plugin-transform-template-literals/test/fixtures/default/ts-literal-type/options.json new file mode 100644 index 000000000000..52b9150873a8 --- /dev/null +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/ts-literal-type/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-template-literals", "syntax-typescript"] +} diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/ts-literal-type/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/ts-literal-type/output.js new file mode 100644 index 000000000000..65e2a36d96eb --- /dev/null +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/ts-literal-type/output.js @@ -0,0 +1,2 @@ +type World = "world"; +type Greeting = `hello ${World}`;