Skip to content

Commit

Permalink
add test for and fix nodejs example
Browse files Browse the repository at this point in the history
  • Loading branch information
frigus02 committed Dec 13, 2023
1 parent dd563e8 commit fa03480
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3607,7 +3607,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

// Look at 'compilerOptions.isolatedModules' and not 'getIsolatedModules(...)' (which considers 'verbatimModuleSyntax')
// here because 'verbatimModuleSyntax' will already have an error for importing a type without 'import type'.
if (compilerOptions.isolatedModules && result && isInExternalModule && meaning & SymbolFlags.Value) {
if (compilerOptions.isolatedModules && result && isInExternalModule && (meaning & SymbolFlags.Value) === SymbolFlags.Value) {
const isGlobal = lookup(globals, name, meaning) === result;
const nonValueSymbol = isGlobal && isSourceFile(lastLocation!) && lastLocation.locals && lookup(lastLocation.locals, name, ~SymbolFlags.Value);
if (nonValueSymbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when
bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.


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

==== ./node.d.ts (0 errors) ====
declare module 'node:console' {
global {
interface Console {
Console: console.ConsoleConstructor;
}
namespace console {
interface ConsoleConstructor {
prototype: Console;
new (): Console;
}
}
var console: Console;
}
export = globalThis.console;
}

==== ./bad.ts (4 errors) ====
import { Date, Event } from './types';
~~~~
Expand All @@ -33,13 +51,17 @@ bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-
return new Event('bar') as Event.T;
}

==== ./good.ts (0 errors) ====
==== ./good.ts (1 errors) ====
import type { Date, Event } from './types';
import { Console } from 'node:console';
~~~~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}
const baz: Console = new Console();

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in t
export type T = any;
}

==== ./node.d.ts (0 errors) ====
declare module 'node:console' {
global {
interface Console {
Console: console.ConsoleConstructor;
}
namespace console {
interface ConsoleConstructor {
prototype: Console;
new (): Console;
}
}
var console: Console;
}
export = globalThis.console;
}

==== ./bad.ts (2 errors) ====
import { Date, Event } from './types';
~~~~
Expand All @@ -29,11 +46,13 @@ bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in t

==== ./good.ts (0 errors) ====
import type { Date, Event } from './types';
import { Console } from 'node:console';
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}
const baz: Console = new Console();

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


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

==== ./node.d.ts (0 errors) ====
declare module 'node:console' {
global {
interface Console {
Console: console.ConsoleConstructor;
}
namespace console {
interface ConsoleConstructor {
prototype: Console;
new (): Console;
}
}
var console: Console;
}
export = globalThis.console;
}

==== ./bad.ts (6 errors) ====
import { Date, Event } from './types';
~~~~
Expand All @@ -39,13 +57,17 @@ bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in t
return new Event('bar') as Event.T;
}

==== ./good.ts (0 errors) ====
==== ./good.ts (1 errors) ====
import type { Date, Event } from './types';
import { Console } from 'node:console';
~~~~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}
const baz: Console = new Console();

19 changes: 19 additions & 0 deletions tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ export namespace Event {
export type T = any;
}

// @filename: ./node.d.ts
declare module 'node:console' {
global {
interface Console {
Console: console.ConsoleConstructor;
}
namespace console {
interface ConsoleConstructor {
prototype: Console;
new (): Console;
}
}
var console: Console;
}
export = globalThis.console;
}

// @filename: ./bad.ts
import { Date, Event } from './types';
function foo(a: Date) {
Expand All @@ -26,10 +43,12 @@ function bar() {

// @filename: ./good.ts
import type { Date, Event } from './types';
import { Console } from 'node:console';
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}
const baz: Console = new Console();

0 comments on commit fa03480

Please sign in to comment.