Skip to content

Commit dd563e8

Browse files
committed
look for local ~Value instead of Type symbol
1 parent 006cb6a commit dd563e8

5 files changed

+72
-14
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3609,9 +3609,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
36093609
// here because 'verbatimModuleSyntax' will already have an error for importing a type without 'import type'.
36103610
if (compilerOptions.isolatedModules && result && isInExternalModule && meaning & SymbolFlags.Value) {
36113611
const isGlobal = lookup(globals, name, meaning) === result;
3612-
const typeSymbol = isSourceFile(lastLocation!) && lastLocation.locals && lookup(lastLocation.locals, name, SymbolFlags.Type);
3613-
if (isGlobal && typeSymbol) {
3614-
const importDecl = typeSymbol.declarations?.find(d => d.kind === SyntaxKind.ImportSpecifier || d.kind === SyntaxKind.ImportClause || d.kind === SyntaxKind.NamespaceImport || d.kind === SyntaxKind.ImportEqualsDeclaration);
3612+
const nonValueSymbol = isGlobal && isSourceFile(lastLocation!) && lastLocation.locals && lookup(lastLocation.locals, name, ~SymbolFlags.Value);
3613+
if (nonValueSymbol) {
3614+
const importDecl = nonValueSymbol.declarations?.find(d => d.kind === SyntaxKind.ImportSpecifier || d.kind === SyntaxKind.ImportClause || d.kind === SyntaxKind.NamespaceImport || d.kind === SyntaxKind.ImportEqualsDeclaration);
36153615
if (importDecl && !isTypeOnlyImportDeclaration(importDecl)) {
36163616
error(importDecl, Diagnostics.Import_0_conflicts_with_global_value_used_in_this_file_so_must_be_declared_with_a_type_only_import_when_isolatedModules_is_enabled, unescapeLeadingUnderscores(name));
36173617
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
22
bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
3+
bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
4+
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
35

46

57
==== ./types.ts (0 errors) ====
@@ -9,21 +11,35 @@ bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-o
911
year: number;
1012
}
1113

12-
==== ./bad.ts (2 errors) ====
13-
import { Date } from './types';
14+
export namespace Event {
15+
export type T = any;
16+
}
17+
18+
==== ./bad.ts (4 errors) ====
19+
import { Date, Event } from './types';
1420
~~~~
1521
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
1622
~~~~
1723
!!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
24+
~~~~~
25+
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
26+
~~~~~
27+
!!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
1828
function foo(a: Date) {
1929
const b = new Date(a.year, a.month, a.day);
2030
return b.getTime();
2131
}
32+
function bar() {
33+
return new Event('bar') as Event.T;
34+
}
2235

2336
==== ./good.ts (0 errors) ====
24-
import type { Date } from './types';
37+
import type { Date, Event } from './types';
2538
function foo(a: Date) {
2639
const b = new Date(a.year, a.month, a.day);
2740
return b.getTime();
2841
}
42+
function bar() {
43+
return new Event('bar') as Event.T;
44+
}
2945

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
2+
bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
23

34

45
==== ./types.ts (0 errors) ====
@@ -8,19 +9,31 @@ bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in th
89
year: number;
910
}
1011

11-
==== ./bad.ts (1 errors) ====
12-
import { Date } from './types';
12+
export namespace Event {
13+
export type T = any;
14+
}
15+
16+
==== ./bad.ts (2 errors) ====
17+
import { Date, Event } from './types';
1318
~~~~
1419
!!! error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
20+
~~~~~
21+
!!! error TS2866: Import 'Event' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
1522
function foo(a: Date) {
1623
const b = new Date(a.year, a.month, a.day);
1724
return b.getTime();
1825
}
26+
function bar() {
27+
return new Event('bar') as Event.T;
28+
}
1929

2030
==== ./good.ts (0 errors) ====
21-
import type { Date } from './types';
31+
import type { Date, Event } from './types';
2232
function foo(a: Date) {
2333
const b = new Date(a.year, a.month, a.day);
2434
return b.getTime();
2535
}
36+
function bar() {
37+
return new Event('bar') as Event.T;
38+
}
2639

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
22
bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
33
bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
4+
bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
5+
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
6+
bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
47

58

69
==== ./types.ts (0 errors) ====
@@ -10,23 +13,39 @@ bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in th
1013
year: number;
1114
}
1215

13-
==== ./bad.ts (3 errors) ====
14-
import { Date } from './types';
16+
export namespace Event {
17+
export type T = any;
18+
}
19+
20+
==== ./bad.ts (6 errors) ====
21+
import { Date, Event } from './types';
1522
~~~~
1623
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
1724
~~~~
1825
!!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
1926
~~~~
2027
!!! error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
28+
~~~~~
29+
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
30+
~~~~~
31+
!!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
32+
~~~~~
33+
!!! error TS2866: Import 'Event' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
2134
function foo(a: Date) {
2235
const b = new Date(a.year, a.month, a.day);
2336
return b.getTime();
2437
}
38+
function bar() {
39+
return new Event('bar') as Event.T;
40+
}
2541

2642
==== ./good.ts (0 errors) ====
27-
import type { Date } from './types';
43+
import type { Date, Event } from './types';
2844
function foo(a: Date) {
2945
const b = new Date(a.year, a.month, a.day);
3046
return b.getTime();
3147
}
48+
function bar() {
49+
return new Event('bar') as Event.T;
50+
}
3251

tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@ export interface Date {
1010
year: number;
1111
}
1212

13+
export namespace Event {
14+
export type T = any;
15+
}
16+
1317
// @filename: ./bad.ts
14-
import { Date } from './types';
18+
import { Date, Event } from './types';
1519
function foo(a: Date) {
1620
const b = new Date(a.year, a.month, a.day);
1721
return b.getTime();
1822
}
23+
function bar() {
24+
return new Event('bar') as Event.T;
25+
}
1926

2027
// @filename: ./good.ts
21-
import type { Date } from './types';
28+
import type { Date, Event } from './types';
2229
function foo(a: Date) {
2330
const b = new Date(a.year, a.month, a.day);
2431
return b.getTime();
2532
}
33+
function bar() {
34+
return new Event('bar') as Event.T;
35+
}

0 commit comments

Comments
 (0)