diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index eb0f943118abc..9ccd81ada5204 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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) { diff --git a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt index 5496cfaeac21b..09ca32e0905e6 100644 --- a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt @@ -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) ==== @@ -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'; ~~~~ @@ -33,8 +51,11 @@ 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(); @@ -42,4 +63,5 @@ bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type- function bar() { return new Event('bar') as Event.T; } + const baz: Console = new Console(); \ No newline at end of file diff --git a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt index 16481e3225d90..2c91eac390a77 100644 --- a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt @@ -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'; ~~~~ @@ -29,6 +46,7 @@ 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(); @@ -36,4 +54,5 @@ bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in t function bar() { return new Event('bar') as Event.T; } + const baz: Console = new Console(); \ No newline at end of file diff --git a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt index f1caa53710923..deb401b76857d 100644 --- a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt @@ -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) ==== @@ -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'; ~~~~ @@ -39,8 +57,11 @@ 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(); @@ -48,4 +69,5 @@ bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in t function bar() { return new Event('bar') as Event.T; } + const baz: Console = new Console(); \ No newline at end of file diff --git a/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts b/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts index 5dc213cb523ee..9b0b7bd7c65e8 100644 --- a/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts +++ b/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts @@ -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) { @@ -26,6 +43,7 @@ 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(); @@ -33,3 +51,4 @@ function foo(a: Date) { function bar() { return new Event('bar') as Event.T; } +const baz: Console = new Console();