From 006cb6a5315e3e423062718ac800712b6338ba64 Mon Sep 17 00:00:00 2001 From: Jan Kuehle Date: Fri, 8 Dec 2023 14:48:02 +0000 Subject: [PATCH 1/3] isolatedModules error on global shadowed by imported type --- src/compiler/checker.ts | 14 ++++++++ src/compiler/diagnosticMessages.json | 4 +++ ...alse,verbatimmodulesyntax=true).errors.txt | 29 +++++++++++++++++ ...rue,verbatimmodulesyntax=false).errors.txt | 26 +++++++++++++++ ...true,verbatimmodulesyntax=true).errors.txt | 32 +++++++++++++++++++ ...isolatedModulesShadowGlobalTypeNotValue.ts | 25 +++++++++++++++ 6 files changed, 130 insertions(+) create mode 100644 tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt create mode 100644 tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt create mode 100644 tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt create mode 100644 tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b0ea7ea9ffb04..a6c101b8cfa2b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -735,6 +735,7 @@ import { isTypeNode, isTypeNodeKind, isTypeOfExpression, + isTypeOnlyImportDeclaration, isTypeOnlyImportOrExportDeclaration, isTypeOperatorNode, isTypeParameterDeclaration, @@ -3603,6 +3604,19 @@ 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) { + const isGlobal = lookup(globals, name, meaning) === result; + const typeSymbol = isSourceFile(lastLocation!) && lastLocation.locals && lookup(lastLocation.locals, name, SymbolFlags.Type); + if (isGlobal && typeSymbol) { + const importDecl = typeSymbol.declarations?.find(d => d.kind === SyntaxKind.ImportSpecifier || d.kind === SyntaxKind.ImportClause || d.kind === SyntaxKind.NamespaceImport || d.kind === SyntaxKind.ImportEqualsDeclaration); + if (importDecl && !isTypeOnlyImportDeclaration(importDecl)) { + 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)); + } + } + } }); } return result; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 80c55bd7ef277..8a16147b63381 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3731,6 +3731,10 @@ "category": "Error", "code": 2865 }, + "Import '{0}' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.": { + "category": "Error", + "code": 2866 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt new file mode 100644 index 0000000000000..776c12d50a6b3 --- /dev/null +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt @@ -0,0 +1,29 @@ +bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. + + +==== ./types.ts (0 errors) ==== + export interface Date { + day: number; + month: number; + year: number; + } + +==== ./bad.ts (2 errors) ==== + import { Date } from './types'; + ~~~~ +!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. + ~~~~ +!!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. + function foo(a: Date) { + const b = new Date(a.year, a.month, a.day); + return b.getTime(); + } + +==== ./good.ts (0 errors) ==== + import type { Date } from './types'; + function foo(a: Date) { + const b = new Date(a.year, a.month, a.day); + return b.getTime(); + } + \ 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 new file mode 100644 index 0000000000000..0331ec2a70b63 --- /dev/null +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt @@ -0,0 +1,26 @@ +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. + + +==== ./types.ts (0 errors) ==== + export interface Date { + day: number; + month: number; + year: number; + } + +==== ./bad.ts (1 errors) ==== + import { Date } from './types'; + ~~~~ +!!! 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. + function foo(a: Date) { + const b = new Date(a.year, a.month, a.day); + return b.getTime(); + } + +==== ./good.ts (0 errors) ==== + import type { Date } from './types'; + function foo(a: Date) { + const b = new Date(a.year, a.month, a.day); + return b.getTime(); + } + \ 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 new file mode 100644 index 0000000000000..968b397731c0e --- /dev/null +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt @@ -0,0 +1,32 @@ +bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +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,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. + + +==== ./types.ts (0 errors) ==== + export interface Date { + day: number; + month: number; + year: number; + } + +==== ./bad.ts (3 errors) ==== + import { Date } from './types'; + ~~~~ +!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. + ~~~~ +!!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. + ~~~~ +!!! 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. + function foo(a: Date) { + const b = new Date(a.year, a.month, a.day); + return b.getTime(); + } + +==== ./good.ts (0 errors) ==== + import type { Date } from './types'; + function foo(a: Date) { + const b = new Date(a.year, a.month, a.day); + return b.getTime(); + } + \ No newline at end of file diff --git a/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts b/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts new file mode 100644 index 0000000000000..6bf46d2a225eb --- /dev/null +++ b/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts @@ -0,0 +1,25 @@ +// @isolatedModules: false, true +// @verbatimModuleSyntax: false, true +// @noEmit: true +// @noTypesAndSymbols: true + +// @filename: ./types.ts +export interface Date { + day: number; + month: number; + year: number; +} + +// @filename: ./bad.ts +import { Date } from './types'; +function foo(a: Date) { + const b = new Date(a.year, a.month, a.day); + return b.getTime(); +} + +// @filename: ./good.ts +import type { Date } from './types'; +function foo(a: Date) { + const b = new Date(a.year, a.month, a.day); + return b.getTime(); +} From dd563e87bf9ecf8ad7d9848093307c62a2df8530 Mon Sep 17 00:00:00 2001 From: Jan Kuehle Date: Tue, 12 Dec 2023 09:59:30 +0000 Subject: [PATCH 2/3] look for local ~Value instead of Type symbol --- src/compiler/checker.ts | 6 ++--- ...alse,verbatimmodulesyntax=true).errors.txt | 22 +++++++++++++--- ...rue,verbatimmodulesyntax=false).errors.txt | 19 +++++++++++--- ...true,verbatimmodulesyntax=true).errors.txt | 25 ++++++++++++++++--- ...isolatedModulesShadowGlobalTypeNotValue.ts | 14 +++++++++-- 5 files changed, 72 insertions(+), 14 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a6c101b8cfa2b..eb0f943118abc 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3609,9 +3609,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { // here because 'verbatimModuleSyntax' will already have an error for importing a type without 'import type'. if (compilerOptions.isolatedModules && result && isInExternalModule && meaning & SymbolFlags.Value) { const isGlobal = lookup(globals, name, meaning) === result; - const typeSymbol = isSourceFile(lastLocation!) && lastLocation.locals && lookup(lastLocation.locals, name, SymbolFlags.Type); - if (isGlobal && typeSymbol) { - const importDecl = typeSymbol.declarations?.find(d => d.kind === SyntaxKind.ImportSpecifier || d.kind === SyntaxKind.ImportClause || d.kind === SyntaxKind.NamespaceImport || d.kind === SyntaxKind.ImportEqualsDeclaration); + const nonValueSymbol = isGlobal && isSourceFile(lastLocation!) && lastLocation.locals && lookup(lastLocation.locals, name, ~SymbolFlags.Value); + if (nonValueSymbol) { + const importDecl = nonValueSymbol.declarations?.find(d => d.kind === SyntaxKind.ImportSpecifier || d.kind === SyntaxKind.ImportClause || d.kind === SyntaxKind.NamespaceImport || d.kind === SyntaxKind.ImportEqualsDeclaration); if (importDecl && !isTypeOnlyImportDeclaration(importDecl)) { 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)); } 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 776c12d50a6b3..5496cfaeac21b 100644 --- a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt @@ -1,5 +1,7 @@ bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. 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. ==== ./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 year: number; } -==== ./bad.ts (2 errors) ==== - import { Date } from './types'; + export namespace Event { + export type T = any; + } + +==== ./bad.ts (4 errors) ==== + import { Date, Event } from './types'; ~~~~ !!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. ~~~~ !!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. + ~~~~~ +!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. + ~~~~~ +!!! error TS1484: 'Event' is a type and must be imported using a type-only import 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; + } ==== ./good.ts (0 errors) ==== - import type { Date } from './types'; + import type { Date, Event } from './types'; 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; + } \ 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 0331ec2a70b63..16481e3225d90 100644 --- a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt @@ -1,4 +1,5 @@ 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. +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. ==== ./types.ts (0 errors) ==== @@ -8,19 +9,31 @@ bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in th year: number; } -==== ./bad.ts (1 errors) ==== - import { Date } from './types'; + export namespace Event { + export type T = any; + } + +==== ./bad.ts (2 errors) ==== + import { Date, Event } from './types'; ~~~~ !!! 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. + ~~~~~ +!!! 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. 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; + } ==== ./good.ts (0 errors) ==== - import type { Date } from './types'; + import type { Date, Event } from './types'; 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; + } \ 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 968b397731c0e..f1caa53710923 100644 --- a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt @@ -1,6 +1,9 @@ bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. 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,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. +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. ==== ./types.ts (0 errors) ==== @@ -10,23 +13,39 @@ bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in th year: number; } -==== ./bad.ts (3 errors) ==== - import { Date } from './types'; + export namespace Event { + export type T = any; + } + +==== ./bad.ts (6 errors) ==== + import { Date, Event } from './types'; ~~~~ !!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. ~~~~ !!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. ~~~~ !!! 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. + ~~~~~ +!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. + ~~~~~ +!!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. + ~~~~~ +!!! 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. 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; + } ==== ./good.ts (0 errors) ==== - import type { Date } from './types'; + import type { Date, Event } from './types'; 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; + } \ No newline at end of file diff --git a/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts b/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts index 6bf46d2a225eb..5dc213cb523ee 100644 --- a/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts +++ b/tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts @@ -10,16 +10,26 @@ export interface Date { year: number; } +export namespace Event { + export type T = any; +} + // @filename: ./bad.ts -import { Date } from './types'; +import { Date, Event } from './types'; 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; +} // @filename: ./good.ts -import type { Date } from './types'; +import type { Date, Event } from './types'; 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; +} From fa034808668e6c00c13e018dfcea18e3c275093f Mon Sep 17 00:00:00 2001 From: Jan Kuehle Date: Wed, 13 Dec 2023 10:33:36 +0000 Subject: [PATCH 3/3] add test for and fix nodejs example --- src/compiler/checker.ts | 2 +- ...alse,verbatimmodulesyntax=true).errors.txt | 24 ++++++++++++++++++- ...rue,verbatimmodulesyntax=false).errors.txt | 19 +++++++++++++++ ...true,verbatimmodulesyntax=true).errors.txt | 24 ++++++++++++++++++- ...isolatedModulesShadowGlobalTypeNotValue.ts | 19 +++++++++++++++ 5 files changed, 85 insertions(+), 3 deletions(-) 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();