Skip to content

Commit

Permalink
isolatedModules error on alias merging with local value (#56354)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch authored Nov 15, 2023
1 parent e40730f commit a636797
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45427,6 +45427,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
Diagnostics.Import_declaration_conflicts_with_local_declaration_of_0;
error(node, message, symbolToString(symbol));
}
else if (node.kind !== SyntaxKind.ExportSpecifier) {
// 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'.
const appearsValueyToTranspiler = compilerOptions.isolatedModules && !findAncestor(node, isTypeOnlyImportOrExportDeclaration);
if (appearsValueyToTranspiler && symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) {
error(
node,
Diagnostics.Import_0_conflicts_with_local_value_so_must_be_declared_with_a_type_only_import_when_isolatedModules_is_enabled,
symbolToString(symbol),
isolatedModulesLikeFlagName,
);
}
}

if (
getIsolatedModules(compilerOptions)
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3711,6 +3711,10 @@
"category": "Error",
"code": 2864
},
"Import '{0}' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.": {
"category": "Error",
"code": 2865
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
1 change: 1 addition & 0 deletions src/testRunner/compilerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class CompilerTest {
"importHelpers",
"downlevelIteration",
"isolatedModules",
"verbatimModuleSyntax",
"strict",
"noImplicitAny",
"strictNullChecks",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.


==== types.ts (0 errors) ====
export type FC = () => void;

==== bad.ts (2 errors) ====
import { FC } from "./types";
~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
~~
!!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
let FC: FC | null = null;

==== good.ts (0 errors) ====
import type { FC } from "./types";
let FC: FC | null = null;

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.


==== types.ts (0 errors) ====
export type FC = () => void;

==== bad.ts (1 errors) ====
import { FC } from "./types";
~~
!!! error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.
let FC: FC | null = null;

==== good.ts (0 errors) ====
import type { FC } from "./types";
let FC: FC | null = null;

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.


==== types.ts (0 errors) ====
export type FC = () => void;

==== bad.ts (3 errors) ====
import { FC } from "./types";
~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
~~
!!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
~~
!!! error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.
let FC: FC | null = null;

==== good.ts (0 errors) ====
import type { FC } from "./types";
let FC: FC | null = null;

15 changes: 15 additions & 0 deletions tests/cases/compiler/isolatedModulesSketchyAliasLocalMerge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @isolatedModules: false, true
// @verbatimModuleSyntax: false, true
// @noEmit: true
// @noTypesAndSymbols: true

// @Filename: types.ts
export type FC = () => void;

// @Filename: bad.ts
import { FC } from "./types";
let FC: FC | null = null;

// @Filename: good.ts
import type { FC } from "./types";
let FC: FC | null = null;

0 comments on commit a636797

Please sign in to comment.