Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isolatedModules error on alias merging with local value #56354

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45423,6 +45423,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;
Loading