Skip to content

Commit fa03480

Browse files
committed
add test for and fix nodejs example
1 parent dd563e8 commit fa03480

5 files changed

+85
-3
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3607,7 +3607,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
36073607

36083608
// Look at 'compilerOptions.isolatedModules' and not 'getIsolatedModules(...)' (which considers 'verbatimModuleSyntax')
36093609
// here because 'verbatimModuleSyntax' will already have an error for importing a type without 'import type'.
3610-
if (compilerOptions.isolatedModules && result && isInExternalModule && meaning & SymbolFlags.Value) {
3610+
if (compilerOptions.isolatedModules && result && isInExternalModule && (meaning & SymbolFlags.Value) === SymbolFlags.Value) {
36113611
const isGlobal = lookup(globals, name, meaning) === result;
36123612
const nonValueSymbol = isGlobal && isSourceFile(lastLocation!) && lastLocation.locals && lookup(lastLocation.locals, name, ~SymbolFlags.Value);
36133613
if (nonValueSymbol) {

tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when
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,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
44
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
5+
good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
56

67

78
==== ./types.ts (0 errors) ====
@@ -15,6 +16,23 @@ bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-
1516
export type T = any;
1617
}
1718

19+
==== ./node.d.ts (0 errors) ====
20+
declare module 'node:console' {
21+
global {
22+
interface Console {
23+
Console: console.ConsoleConstructor;
24+
}
25+
namespace console {
26+
interface ConsoleConstructor {
27+
prototype: Console;
28+
new (): Console;
29+
}
30+
}
31+
var console: Console;
32+
}
33+
export = globalThis.console;
34+
}
35+
1836
==== ./bad.ts (4 errors) ====
1937
import { Date, Event } from './types';
2038
~~~~
@@ -33,13 +51,17 @@ bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-
3351
return new Event('bar') as Event.T;
3452
}
3553

36-
==== ./good.ts (0 errors) ====
54+
==== ./good.ts (1 errors) ====
3755
import type { Date, Event } from './types';
56+
import { Console } from 'node:console';
57+
~~~~~~~
58+
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
3859
function foo(a: Date) {
3960
const b = new Date(a.year, a.month, a.day);
4061
return b.getTime();
4162
}
4263
function bar() {
4364
return new Event('bar') as Event.T;
4465
}
66+
const baz: Console = new Console();
4567

tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in t
1313
export type T = any;
1414
}
1515

16+
==== ./node.d.ts (0 errors) ====
17+
declare module 'node:console' {
18+
global {
19+
interface Console {
20+
Console: console.ConsoleConstructor;
21+
}
22+
namespace console {
23+
interface ConsoleConstructor {
24+
prototype: Console;
25+
new (): Console;
26+
}
27+
}
28+
var console: Console;
29+
}
30+
export = globalThis.console;
31+
}
32+
1633
==== ./bad.ts (2 errors) ====
1734
import { Date, Event } from './types';
1835
~~~~
@@ -29,11 +46,13 @@ bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in t
2946

3047
==== ./good.ts (0 errors) ====
3148
import type { Date, Event } from './types';
49+
import { Console } from 'node:console';
3250
function foo(a: Date) {
3351
const b = new Date(a.year, a.month, a.day);
3452
return b.getTime();
3553
}
3654
function bar() {
3755
return new Event('bar') as Event.T;
3856
}
57+
const baz: Console = new Console();
3958

tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in th
44
bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
55
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
66
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.
7+
good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
78

89

910
==== ./types.ts (0 errors) ====
@@ -17,6 +18,23 @@ bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in t
1718
export type T = any;
1819
}
1920

21+
==== ./node.d.ts (0 errors) ====
22+
declare module 'node:console' {
23+
global {
24+
interface Console {
25+
Console: console.ConsoleConstructor;
26+
}
27+
namespace console {
28+
interface ConsoleConstructor {
29+
prototype: Console;
30+
new (): Console;
31+
}
32+
}
33+
var console: Console;
34+
}
35+
export = globalThis.console;
36+
}
37+
2038
==== ./bad.ts (6 errors) ====
2139
import { Date, Event } from './types';
2240
~~~~
@@ -39,13 +57,17 @@ bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in t
3957
return new Event('bar') as Event.T;
4058
}
4159

42-
==== ./good.ts (0 errors) ====
60+
==== ./good.ts (1 errors) ====
4361
import type { Date, Event } from './types';
62+
import { Console } from 'node:console';
63+
~~~~~~~
64+
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
4465
function foo(a: Date) {
4566
const b = new Date(a.year, a.month, a.day);
4667
return b.getTime();
4768
}
4869
function bar() {
4970
return new Event('bar') as Event.T;
5071
}
72+
const baz: Console = new Console();
5173

tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ export namespace Event {
1414
export type T = any;
1515
}
1616

17+
// @filename: ./node.d.ts
18+
declare module 'node:console' {
19+
global {
20+
interface Console {
21+
Console: console.ConsoleConstructor;
22+
}
23+
namespace console {
24+
interface ConsoleConstructor {
25+
prototype: Console;
26+
new (): Console;
27+
}
28+
}
29+
var console: Console;
30+
}
31+
export = globalThis.console;
32+
}
33+
1734
// @filename: ./bad.ts
1835
import { Date, Event } from './types';
1936
function foo(a: Date) {
@@ -26,10 +43,12 @@ function bar() {
2643

2744
// @filename: ./good.ts
2845
import type { Date, Event } from './types';
46+
import { Console } from 'node:console';
2947
function foo(a: Date) {
3048
const b = new Date(a.year, a.month, a.day);
3149
return b.getTime();
3250
}
3351
function bar() {
3452
return new Event('bar') as Event.T;
3553
}
54+
const baz: Console = new Console();

0 commit comments

Comments
 (0)