Skip to content

Commit

Permalink
Module declarations must use "export declare" instead of "declare exp…
Browse files Browse the repository at this point in the history
…ort". (#8085)
  • Loading branch information
rmja committed May 13, 2022
1 parent 38bb2ee commit fe58766
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
Expand Up @@ -11,5 +11,8 @@ export default function test(params: Params): number;
export function foo(): number;
export var x: number;
export var hi: number;
export declare module mod {
function bar(): number;
}

//# sourceMappingURL=types.d.ts.map
Expand Up @@ -23,3 +23,9 @@ export function foo() {
var x = 2;
var p = x + 2, q = 3;
export {p as hi, x};

export module mod {
export function bar() {
return 2;
}
}
10 changes: 9 additions & 1 deletion packages/transformers/typescript-types/src/shake.js
Expand Up @@ -36,7 +36,15 @@ export function shake(
// Deeply nested module declarations are assumed to be module augmentations and left alone.
if (moduleStack.length >= 1) {
// Since we are hoisting them to the top-level scope, we need to add a "declare" keyword to make them ambient.
node.modifiers.unshift(ts.createModifier(ts.SyntaxKind.DeclareKeyword));
// we also want the declare keyword to come after the export keyword to guarantee a valid typings file.
node.modifiers ??= [];
const index =
node.modifiers[0]?.kind === ts.SyntaxKind.ExportKeyword ? 1 : 0;
node.modifiers.splice(
index,
0,
ts.createModifier(ts.SyntaxKind.DeclareKeyword),
);
return node;
}

Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Expand Up @@ -12708,9 +12708,9 @@ typedarray@^0.0.6:
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@>=3.0.0:
version "4.5.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.3.tgz#afaa858e68c7103317d89eb90c5d8906268d353c"
integrity sha512-eVYaEHALSt+s9LbvgEv4Ef+Tdq7hBiIZgii12xXJnukryt3pMgJf6aKhoCZ3FWQsu6sydEnkg11fYXLzhLBjeQ==
version "4.6.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.4.tgz#caa78bbc3a59e6a5c510d35703f6a09877ce45e9"
integrity sha512-9ia/jWHIEbo49HfjrLGfKbZSuWo9iTMwXO+Ca3pRsSpbsMbc7/IU8NKdCZVRRBafVPGnoJeFL76ZOAA84I9fEg==

uglify-js@^3.1.4:
version "3.7.6"
Expand Down

0 comments on commit fe58766

Please sign in to comment.