Skip to content

Commit c74d25c

Browse files
30837 - Bad error message when default 'import'-ing an 'export =' without 'esModuleInterop' (#30847)
30837 - Bad error message when default 'import'-ing an 'export =' without 'esModuleInterop'
2 parents 53c92d6 + f5d4e66 commit c74d25c

10 files changed

+116
-25
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1966,7 +1966,21 @@ namespace ts {
19661966
const file = find(moduleSymbol.declarations, isSourceFile);
19671967
const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias);
19681968
if (!exportDefaultSymbol && !hasSyntheticDefault) {
1969-
error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol));
1969+
if (hasExportAssignmentSymbol(moduleSymbol)) {
1970+
const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop";
1971+
const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals);
1972+
const exportAssignment = exportEqualsSymbol!.valueDeclaration;
1973+
const err = error(node.name, Diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, symbolToString(moduleSymbol), compilerOptionName);
1974+
1975+
addRelatedInfo(err, createDiagnosticForNode(
1976+
exportAssignment,
1977+
Diagnostics.This_module_is_declared_with_using_export_and_can_only_be_used_with_a_default_import_when_using_the_0_flag,
1978+
compilerOptionName
1979+
));
1980+
}
1981+
else {
1982+
error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol));
1983+
}
19701984
}
19711985
else if (hasSyntheticDefault) {
19721986
// per emit behavior, a synthetic default overrides a "real" .default member if `__esModule` is not present

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,10 @@
851851
"category": "Error",
852852
"code": 1258
853853
},
854+
"Module '{0}' can only be default-imported using the '{1}' flag": {
855+
"category": "Error",
856+
"code": 1259
857+
},
854858
"'with' statements are not allowed in an async function block.": {
855859
"category": "Error",
856860
"code": 1300
@@ -2156,6 +2160,10 @@
21562160
"category": "Error",
21572161
"code": 2593
21582162
},
2163+
"This module is declared with using 'export =', and can only be used with a default import when using the '{0}' flag.": {
2164+
"category": "Error",
2165+
"code": 2594
2166+
},
21592167
"JSX element attributes type '{0}' may not be a union type.": {
21602168
"category": "Error",
21612169
"code": 2600

tests/baselines/reference/allowSyntheticDefaultImports6.errors.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/a.ts(1,8): error TS1192: Module '"tests/cases/compiler/b"' has no default export.
1+
tests/cases/compiler/a.ts(1,8): error TS1259: Module '"tests/cases/compiler/b"' can only be default-imported using the 'esModuleInterop' flag
22

33

44
==== tests/cases/compiler/b.d.ts (0 errors) ====
@@ -10,6 +10,7 @@ tests/cases/compiler/a.ts(1,8): error TS1192: Module '"tests/cases/compiler/b"'
1010
==== tests/cases/compiler/a.ts (1 errors) ====
1111
import Foo from "./b";
1212
~~~
13-
!!! error TS1192: Module '"tests/cases/compiler/b"' has no default export.
13+
!!! error TS1259: Module '"tests/cases/compiler/b"' can only be default-imported using the 'esModuleInterop' flag
14+
!!! related TS2594 tests/cases/compiler/b.d.ts:4:1: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
1415
export var x = new Foo();
1516

tests/baselines/reference/es6ExportEqualsInterop.errors.txt

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
tests/cases/compiler/main.ts(15,1): error TS2693: 'z1' only refers to a type, but is being used as a value here.
22
tests/cases/compiler/main.ts(21,4): error TS2339: Property 'a' does not exist on type '() => any'.
33
tests/cases/compiler/main.ts(23,4): error TS2339: Property 'a' does not exist on type 'typeof Foo'.
4-
tests/cases/compiler/main.ts(27,8): error TS1192: Module '"interface"' has no default export.
5-
tests/cases/compiler/main.ts(28,8): error TS1192: Module '"variable"' has no default export.
6-
tests/cases/compiler/main.ts(29,8): error TS1192: Module '"interface-variable"' has no default export.
7-
tests/cases/compiler/main.ts(30,8): error TS1192: Module '"module"' has no default export.
8-
tests/cases/compiler/main.ts(31,8): error TS1192: Module '"interface-module"' has no default export.
9-
tests/cases/compiler/main.ts(32,8): error TS1192: Module '"variable-module"' has no default export.
10-
tests/cases/compiler/main.ts(33,8): error TS1192: Module '"function"' has no default export.
11-
tests/cases/compiler/main.ts(34,8): error TS1192: Module '"function-module"' has no default export.
12-
tests/cases/compiler/main.ts(35,8): error TS1192: Module '"class"' has no default export.
13-
tests/cases/compiler/main.ts(36,8): error TS1192: Module '"class-module"' has no default export.
4+
tests/cases/compiler/main.ts(27,8): error TS1259: Module '"interface"' can only be default-imported using the 'esModuleInterop' flag
5+
tests/cases/compiler/main.ts(28,8): error TS1259: Module '"variable"' can only be default-imported using the 'esModuleInterop' flag
6+
tests/cases/compiler/main.ts(29,8): error TS1259: Module '"interface-variable"' can only be default-imported using the 'esModuleInterop' flag
7+
tests/cases/compiler/main.ts(30,8): error TS1259: Module '"module"' can only be default-imported using the 'esModuleInterop' flag
8+
tests/cases/compiler/main.ts(31,8): error TS1259: Module '"interface-module"' can only be default-imported using the 'esModuleInterop' flag
9+
tests/cases/compiler/main.ts(32,8): error TS1259: Module '"variable-module"' can only be default-imported using the 'esModuleInterop' flag
10+
tests/cases/compiler/main.ts(33,8): error TS1259: Module '"function"' can only be default-imported using the 'esModuleInterop' flag
11+
tests/cases/compiler/main.ts(34,8): error TS1259: Module '"function-module"' can only be default-imported using the 'esModuleInterop' flag
12+
tests/cases/compiler/main.ts(35,8): error TS1259: Module '"class"' can only be default-imported using the 'esModuleInterop' flag
13+
tests/cases/compiler/main.ts(36,8): error TS1259: Module '"class-module"' can only be default-imported using the 'esModuleInterop' flag
1414
tests/cases/compiler/main.ts(39,21): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.
1515
tests/cases/compiler/main.ts(45,21): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.
1616
tests/cases/compiler/main.ts(47,21): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.
@@ -76,34 +76,44 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses
7676
// default import
7777
import x1 from "interface";
7878
~~
79-
!!! error TS1192: Module '"interface"' has no default export.
79+
!!! error TS1259: Module '"interface"' can only be default-imported using the 'esModuleInterop' flag
80+
!!! related TS2594 tests/cases/compiler/modules.d.ts:6:5: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
8081
import x2 from "variable";
8182
~~
82-
!!! error TS1192: Module '"variable"' has no default export.
83+
!!! error TS1259: Module '"variable"' can only be default-imported using the 'esModuleInterop' flag
84+
!!! related TS2594 tests/cases/compiler/modules.d.ts:14:5: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
8385
import x3 from "interface-variable";
8486
~~
85-
!!! error TS1192: Module '"interface-variable"' has no default export.
87+
!!! error TS1259: Module '"interface-variable"' can only be default-imported using the 'esModuleInterop' flag
88+
!!! related TS2594 tests/cases/compiler/modules.d.ts:26:5: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
8689
import x4 from "module";
8790
~~
88-
!!! error TS1192: Module '"module"' has no default export.
91+
!!! error TS1259: Module '"module"' can only be default-imported using the 'esModuleInterop' flag
92+
!!! related TS2594 tests/cases/compiler/modules.d.ts:34:5: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
8993
import x5 from "interface-module";
9094
~~
91-
!!! error TS1192: Module '"interface-module"' has no default export.
95+
!!! error TS1259: Module '"interface-module"' can only be default-imported using the 'esModuleInterop' flag
96+
!!! related TS2594 tests/cases/compiler/modules.d.ts:46:5: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
9297
import x6 from "variable-module";
9398
~~
94-
!!! error TS1192: Module '"variable-module"' has no default export.
99+
!!! error TS1259: Module '"variable-module"' can only be default-imported using the 'esModuleInterop' flag
100+
!!! related TS2594 tests/cases/compiler/modules.d.ts:60:5: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
95101
import x7 from "function";
96102
~~
97-
!!! error TS1192: Module '"function"' has no default export.
103+
!!! error TS1259: Module '"function"' can only be default-imported using the 'esModuleInterop' flag
104+
!!! related TS2594 tests/cases/compiler/modules.d.ts:65:5: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
98105
import x8 from "function-module";
99106
~~
100-
!!! error TS1192: Module '"function-module"' has no default export.
107+
!!! error TS1259: Module '"function-module"' can only be default-imported using the 'esModuleInterop' flag
108+
!!! related TS2594 tests/cases/compiler/modules.d.ts:74:5: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
101109
import x9 from "class";
102110
~~
103-
!!! error TS1192: Module '"class"' has no default export.
111+
!!! error TS1259: Module '"class"' can only be default-imported using the 'esModuleInterop' flag
112+
!!! related TS2594 tests/cases/compiler/modules.d.ts:82:5: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
104113
import x0 from "class-module";
105114
~~
106-
!!! error TS1192: Module '"class-module"' has no default export.
115+
!!! error TS1259: Module '"class-module"' can only be default-imported using the 'esModuleInterop' flag
116+
!!! related TS2594 tests/cases/compiler/modules.d.ts:94:5: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
107117

108118
// namespace import
109119
import * as y1 from "interface";
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/es6ImportDefaultBindingInEs5_1.ts(1,8): error TS1192: Module '"tests/cases/compiler/es6ImportDefaultBindingInEs5_0"' has no default export.
1+
tests/cases/compiler/es6ImportDefaultBindingInEs5_1.ts(1,8): error TS1259: Module '"tests/cases/compiler/es6ImportDefaultBindingInEs5_0"' can only be default-imported using the 'esModuleInterop' flag
22

33

44
==== tests/cases/compiler/es6ImportDefaultBindingInEs5_0.ts (0 errors) ====
@@ -8,4 +8,5 @@ tests/cases/compiler/es6ImportDefaultBindingInEs5_1.ts(1,8): error TS1192: Modul
88
==== tests/cases/compiler/es6ImportDefaultBindingInEs5_1.ts (1 errors) ====
99
import defaultBinding from "./es6ImportDefaultBindingInEs5_0";
1010
~~~~~~~~~~~~~~
11-
!!! error TS1192: Module '"tests/cases/compiler/es6ImportDefaultBindingInEs5_0"' has no default export.
11+
!!! error TS1259: Module '"tests/cases/compiler/es6ImportDefaultBindingInEs5_0"' can only be default-imported using the 'esModuleInterop' flag
12+
!!! related TS2594 tests/cases/compiler/es6ImportDefaultBindingInEs5_0.ts:2:1: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/bar.ts(1,1): error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead.
2+
/foo.ts(1,8): error TS1259: Module '"/bar"' can only be default-imported using the 'allowSyntheticDefaultImports' flag
3+
4+
5+
==== /bar.ts (1 errors) ====
6+
export = bar;
7+
~~~~~~~~~~~~~
8+
!!! error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead.
9+
function bar() {}
10+
11+
==== /foo.ts (1 errors) ====
12+
import bar from './bar';
13+
~~~
14+
!!! error TS1259: Module '"/bar"' can only be default-imported using the 'allowSyntheticDefaultImports' flag
15+
!!! related TS2594 /bar.ts:1:1: This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/exportAssignmentWithoutAllowSyntheticDefaultImportsError.ts] ////
2+
3+
//// [bar.ts]
4+
export = bar;
5+
function bar() {}
6+
7+
//// [foo.ts]
8+
import bar from './bar';
9+
10+
//// [bar.js]
11+
function bar() { }
12+
//// [foo.js]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== /bar.ts ===
2+
export = bar;
3+
>bar : Symbol(bar, Decl(bar.ts, 0, 13))
4+
5+
function bar() {}
6+
>bar : Symbol(bar, Decl(bar.ts, 0, 13))
7+
8+
=== /foo.ts ===
9+
import bar from './bar';
10+
>bar : Symbol(bar, Decl(foo.ts, 0, 6))
11+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== /bar.ts ===
2+
export = bar;
3+
>bar : () => void
4+
5+
function bar() {}
6+
>bar : () => void
7+
8+
=== /foo.ts ===
9+
import bar from './bar';
10+
>bar : any
11+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @module: es2015
2+
3+
// @Filename: /bar.ts
4+
export = bar;
5+
function bar() {}
6+
7+
// @Filename: /foo.ts
8+
import bar from './bar';

0 commit comments

Comments
 (0)