Skip to content

Commit 11bc7b7

Browse files
authored
Issue error on export= in esm mode declaration file (#52109)
1 parent 4b69e13 commit 11bc7b7

6 files changed

+76
-5
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3903,7 +3903,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
39033903
const hasDefaultOnly = isOnlyImportedAsDefault(specifier);
39043904
const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, specifier);
39053905
if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) {
3906-
if (hasExportAssignmentSymbol(moduleSymbol)) {
3906+
if (hasExportAssignmentSymbol(moduleSymbol) && !(getAllowSyntheticDefaultImports(compilerOptions) || getESModuleInterop(compilerOptions))) {
39073907
const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop";
39083908
const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals);
39093909
const exportAssignment = exportEqualsSymbol!.valueDeclaration;
@@ -43100,16 +43100,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4310043100
grammarErrorOnNode(node.expression, Diagnostics.The_expression_of_an_export_assignment_must_be_an_identifier_or_qualified_name_in_an_ambient_context);
4310143101
}
4310243102

43103-
if (node.isExportEquals && !(node.flags & NodeFlags.Ambient)) {
43104-
if (moduleKind >= ModuleKind.ES2015 && getSourceFileOfNode(node).impliedNodeFormat !== ModuleKind.CommonJS) {
43103+
if (node.isExportEquals) {
43104+
// Forbid export= in esm implementation files, and esm mode declaration files
43105+
if (moduleKind >= ModuleKind.ES2015 &&
43106+
((node.flags & NodeFlags.Ambient && getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.ESNext) ||
43107+
(!(node.flags & NodeFlags.Ambient) && getSourceFileOfNode(node).impliedNodeFormat !== ModuleKind.CommonJS))) {
4310543108
// export assignment is not supported in es6 modules
4310643109
grammarErrorOnNode(node, Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_modules_Consider_using_export_default_or_another_module_format_instead);
4310743110
}
43108-
else if (moduleKind === ModuleKind.System) {
43111+
else if (moduleKind === ModuleKind.System && !(node.flags & NodeFlags.Ambient)) {
4310943112
// system modules does not support export assignment
4311043113
grammarErrorOnNode(node, Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system);
4311143114
}
43112-
else if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler) {
43115+
else if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler && !(node.flags & NodeFlags.Ambient)) {
4311343116
grammarErrorOnNode(node, Diagnostics.Export_assignment_cannot_be_used_when_moduleResolution_is_set_to_bundler_Consider_using_export_default_or_another_module_format_instead);
4311443117
}
4311543118
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/main.mts(1,8): error TS1192: Module '"tests/cases/compiler/other"' has no default export.
2+
tests/cases/compiler/other.d.mts(2,1): error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead.
3+
4+
5+
==== tests/cases/compiler/other.d.mts (1 errors) ====
6+
declare function example(): 5;
7+
export = example;
8+
~~~~~~~~~~~~~~~~~
9+
!!! error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead.
10+
11+
==== tests/cases/compiler/main.mts (1 errors) ====
12+
import example from "./other.mjs";
13+
~~~~~~~
14+
!!! error TS1192: Module '"tests/cases/compiler/other"' has no default export.
15+
example();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/compiler/esmModeDeclarationFileWithExportAssignment.ts] ////
2+
3+
//// [other.d.mts]
4+
declare function example(): 5;
5+
export = example;
6+
7+
//// [main.mts]
8+
import example from "./other.mjs";
9+
example();
10+
11+
//// [main.mjs]
12+
import example from "./other.mjs";
13+
example();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/other.d.mts ===
2+
declare function example(): 5;
3+
>example : Symbol(example, Decl(other.d.mts, 0, 0))
4+
5+
export = example;
6+
>example : Symbol(example, Decl(other.d.mts, 0, 0))
7+
8+
=== tests/cases/compiler/main.mts ===
9+
import example from "./other.mjs";
10+
>example : Symbol(example, Decl(main.mts, 0, 6))
11+
12+
example();
13+
>example : Symbol(example, Decl(main.mts, 0, 6))
14+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/other.d.mts ===
2+
declare function example(): 5;
3+
>example : () => 5
4+
5+
export = example;
6+
>example : () => 5
7+
8+
=== tests/cases/compiler/main.mts ===
9+
import example from "./other.mjs";
10+
>example : any
11+
12+
example();
13+
>example() : any
14+
>example : any
15+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @target: esnext
2+
// @module: nodenext
3+
// @allowSyntheticDefaultImports: true
4+
// @strict: true
5+
// @filename: other.d.mts
6+
declare function example(): 5;
7+
export = example;
8+
9+
// @filename: main.mts
10+
import example from "./other.mjs";
11+
example();

0 commit comments

Comments
 (0)