diff --git a/packages/babel-plugin-transform-typescript/src/namespace.ts b/packages/babel-plugin-transform-typescript/src/namespace.ts index 74532eca9412..a181cdca7448 100644 --- a/packages/babel-plugin-transform-typescript/src/namespace.ts +++ b/packages/babel-plugin-transform-typescript/src/namespace.ts @@ -81,11 +81,14 @@ function handleVariableDeclaration( // `export const a = 1` transforms to `const a = N.a = 1`, the output // is smaller than `const a = 1; N.a = a`; for (const declarator of declarations) { - declarator.init = t.assignmentExpression( - "=", - getMemberExpression(name, declarator.id.name), - declarator.init, - ); + // `export declare const a`,the declarator.init would be null, + if (declarator.init) { + declarator.init = t.assignmentExpression( + "=", + getMemberExpression(name, declarator.id.name), + declarator.init, + ); + } } return [node]; } @@ -185,6 +188,10 @@ function handleNested( case "TSEnumDeclaration": case "FunctionDeclaration": case "ClassDeclaration": { + if (subNode.declaration.declare) { + continue; + } + const itemName = subNode.declaration.id.name; names.add(itemName); namespaceTopLevel.splice( diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/namespace/declare/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/declare/input.ts new file mode 100644 index 000000000000..8850f9ac5929 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/declare/input.ts @@ -0,0 +1,7 @@ +export namespace N { + export declare class C { + } + export declare enum e { }; + export declare function f(): void; + export declare const v: unknown; +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/namespace/declare/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/declare/output.mjs new file mode 100644 index 000000000000..70db61af014a --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/declare/output.mjs @@ -0,0 +1,5 @@ +export let N; + +(function (_N) { + ; +})(N || (N = {}));