Skip to content

Commit a636797

Browse files
authored
isolatedModules error on alias merging with local value (#56354)
1 parent e40730f commit a636797

7 files changed

+90
-0
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45427,6 +45427,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4542745427
Diagnostics.Import_declaration_conflicts_with_local_declaration_of_0;
4542845428
error(node, message, symbolToString(symbol));
4542945429
}
45430+
else if (node.kind !== SyntaxKind.ExportSpecifier) {
45431+
// Look at 'compilerOptions.isolatedModules' and not 'getIsolatedModules(...)' (which considers 'verbatimModuleSyntax')
45432+
// here because 'verbatimModuleSyntax' will already have an error for importing a type without 'import type'.
45433+
const appearsValueyToTranspiler = compilerOptions.isolatedModules && !findAncestor(node, isTypeOnlyImportOrExportDeclaration);
45434+
if (appearsValueyToTranspiler && symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) {
45435+
error(
45436+
node,
45437+
Diagnostics.Import_0_conflicts_with_local_value_so_must_be_declared_with_a_type_only_import_when_isolatedModules_is_enabled,
45438+
symbolToString(symbol),
45439+
isolatedModulesLikeFlagName,
45440+
);
45441+
}
45442+
}
4543045443

4543145444
if (
4543245445
getIsolatedModules(compilerOptions)

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3711,6 +3711,10 @@
37113711
"category": "Error",
37123712
"code": 2864
37133713
},
3714+
"Import '{0}' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.": {
3715+
"category": "Error",
3716+
"code": 2865
3717+
},
37143718

37153719
"Import declaration '{0}' is using private name '{1}'.": {
37163720
"category": "Error",

src/testRunner/compilerRunner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class CompilerTest {
140140
"importHelpers",
141141
"downlevelIteration",
142142
"isolatedModules",
143+
"verbatimModuleSyntax",
143144
"strict",
144145
"noImplicitAny",
145146
"strictNullChecks",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
2+
bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
3+
4+
5+
==== types.ts (0 errors) ====
6+
export type FC = () => void;
7+
8+
==== bad.ts (2 errors) ====
9+
import { FC } from "./types";
10+
~~
11+
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
12+
~~
13+
!!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
14+
let FC: FC | null = null;
15+
16+
==== good.ts (0 errors) ====
17+
import type { FC } from "./types";
18+
let FC: FC | null = null;
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.
2+
3+
4+
==== types.ts (0 errors) ====
5+
export type FC = () => void;
6+
7+
==== bad.ts (1 errors) ====
8+
import { FC } from "./types";
9+
~~
10+
!!! error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.
11+
let FC: FC | null = null;
12+
13+
==== good.ts (0 errors) ====
14+
import type { FC } from "./types";
15+
let FC: FC | null = null;
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
2+
bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
3+
bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.
4+
5+
6+
==== types.ts (0 errors) ====
7+
export type FC = () => void;
8+
9+
==== bad.ts (3 errors) ====
10+
import { FC } from "./types";
11+
~~
12+
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
13+
~~
14+
!!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
15+
~~
16+
!!! error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.
17+
let FC: FC | null = null;
18+
19+
==== good.ts (0 errors) ====
20+
import type { FC } from "./types";
21+
let FC: FC | null = null;
22+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @isolatedModules: false, true
2+
// @verbatimModuleSyntax: false, true
3+
// @noEmit: true
4+
// @noTypesAndSymbols: true
5+
6+
// @Filename: types.ts
7+
export type FC = () => void;
8+
9+
// @Filename: bad.ts
10+
import { FC } from "./types";
11+
let FC: FC | null = null;
12+
13+
// @Filename: good.ts
14+
import type { FC } from "./types";
15+
let FC: FC | null = null;

0 commit comments

Comments
 (0)