Skip to content

Remove always-incorrect error message about importing .ts extensions #52595

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

Merged
merged 3 commits into from
Feb 3, 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
11 changes: 1 addition & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4961,14 +4961,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
error(errorNode, resolutionDiagnostic, moduleReference, resolvedModule.resolvedFileName);
}
else {
const tsExtension = tryExtractTSExtension(moduleReference);
const isExtensionlessRelativePathImport = pathIsRelative(moduleReference) && !hasExtension(moduleReference);
const resolutionIsNode16OrNext = moduleResolutionKind === ModuleResolutionKind.Node16 ||
moduleResolutionKind === ModuleResolutionKind.NodeNext;
if (tsExtension) {
errorOnTSExtensionImport(tsExtension);
}
else if (!getResolveJsonModule(compilerOptions) &&
if (!getResolveJsonModule(compilerOptions) &&
fileExtensionIs(moduleReference, Extension.Json) &&
moduleResolutionKind !== ModuleResolutionKind.Classic &&
hasJsonModuleEmitEnabled(compilerOptions)) {
Expand All @@ -4993,11 +4989,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
return undefined;

function errorOnTSExtensionImport(tsExtension: string) {
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
error(errorNode, diag, tsExtension, getSuggestedImportSource(tsExtension));
}

function getSuggestedImportSource(tsExtension: string) {
const importSourceWithoutExtension = removeExtension(moduleReference, tsExtension);
/**
Expand Down
4 changes: 0 additions & 4 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3026,10 +3026,6 @@
"category": "Error",
"code": 2690
},
"An import path cannot end with a '{0}' extension. Consider importing '{1}' instead.": {
"category": "Error",
"code": 2691
},
"'{0}' is a primitive, but '{1}' is a wrapper object. Prefer using '{0}' when possible.": {
"category": "Error",
"code": 2692
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/c.ts(1,16): error TS2307: Cannot find module './thisfiledoesnotexist.ts' or its corresponding type declarations.


==== /ts.ts (0 errors) ====
export {};

==== /tsx.tsx (0 errors) ====
export {};

==== /dts.d.ts (0 errors) ====
export {};

==== /b.ts (0 errors) ====
import {} from "./ts.js";
import {} from "./ts.ts";
import type {} from "./ts.d.ts";

import {} from "./tsx.js";
import {} from "./tsx.jsx";
import {} from "./tsx.ts";
import {} from "./tsx.tsx";
import type {} from "./tsx.d.ts";

import {} from "./dts.js";
import {} from "./dts.ts";
import type {} from "./dts.d.ts";

==== /c.ts (1 errors) ====
import {} from "./thisfiledoesnotexist.ts";
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module './thisfiledoesnotexist.ts' or its corresponding type declarations.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/c.ts(1,16): error TS2792: Cannot find module './thisfiledoesnotexist.ts'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?


==== /ts.ts (0 errors) ====
export {};

==== /tsx.tsx (0 errors) ====
export {};

==== /dts.d.ts (0 errors) ====
export {};

==== /b.ts (0 errors) ====
import {} from "./ts.js";
import {} from "./ts.ts";
import type {} from "./ts.d.ts";

import {} from "./tsx.js";
import {} from "./tsx.jsx";
import {} from "./tsx.ts";
import {} from "./tsx.tsx";
import type {} from "./tsx.d.ts";

import {} from "./dts.js";
import {} from "./dts.ts";
import type {} from "./dts.d.ts";

==== /c.ts (1 errors) ====
import {} from "./thisfiledoesnotexist.ts";
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2792: Cannot find module './thisfiledoesnotexist.ts'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this correct error message when the file doesnt exist ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I actually didn’t look close enough at this one. This is weird; I think it’s only supposed to show up for non-relative names.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this message just always comes up in classic, probably because classic is terrible and nobody should use it. It doesn’t make a ton of sense for relative imports, but honestly if someone is on classic it probably is a mistake, so I think we should leave it.


Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/c.ts(1,16): error TS2307: Cannot find module './thisfiledoesnotexist.ts' or its corresponding type declarations.


==== /ts.ts (0 errors) ====
export {};

==== /tsx.tsx (0 errors) ====
export {};

==== /dts.d.ts (0 errors) ====
export {};

==== /b.ts (0 errors) ====
import {} from "./ts.js";
import {} from "./ts.ts";
import type {} from "./ts.d.ts";

import {} from "./tsx.js";
import {} from "./tsx.jsx";
import {} from "./tsx.ts";
import {} from "./tsx.tsx";
import type {} from "./tsx.d.ts";

import {} from "./dts.js";
import {} from "./dts.ts";
import type {} from "./dts.d.ts";

==== /c.ts (1 errors) ====
import {} from "./thisfiledoesnotexist.ts";
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module './thisfiledoesnotexist.ts' or its corresponding type declarations.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/c.ts(1,16): error TS2307: Cannot find module './thisfiledoesnotexist.ts' or its corresponding type declarations.


==== /ts.ts (0 errors) ====
export {};

==== /tsx.tsx (0 errors) ====
export {};

==== /dts.d.ts (0 errors) ====
export {};

==== /b.ts (0 errors) ====
import {} from "./ts.js";
import {} from "./ts.ts";
import type {} from "./ts.d.ts";

import {} from "./tsx.js";
import {} from "./tsx.jsx";
import {} from "./tsx.ts";
import {} from "./tsx.tsx";
import type {} from "./tsx.d.ts";

import {} from "./dts.js";
import {} from "./dts.ts";
import type {} from "./dts.d.ts";

==== /c.ts (1 errors) ====
import {} from "./thisfiledoesnotexist.ts";
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module './thisfiledoesnotexist.ts' or its corresponding type declarations.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/c.ts(1,16): error TS2307: Cannot find module './thisfiledoesnotexist.ts' or its corresponding type declarations.


==== /ts.ts (0 errors) ====
export {};

==== /tsx.tsx (0 errors) ====
export {};

==== /dts.d.ts (0 errors) ====
export {};

==== /b.ts (0 errors) ====
import {} from "./ts.js";
import {} from "./ts.ts";
import type {} from "./ts.d.ts";

import {} from "./tsx.js";
import {} from "./tsx.jsx";
import {} from "./tsx.ts";
import {} from "./tsx.tsx";
import type {} from "./tsx.d.ts";

import {} from "./dts.js";
import {} from "./dts.ts";
import type {} from "./dts.d.ts";

==== /c.ts (1 errors) ====
import {} from "./thisfiledoesnotexist.ts";
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module './thisfiledoesnotexist.ts' or its corresponding type declarations.

32 changes: 16 additions & 16 deletions tests/baselines/reference/declarationFileForTsJsImport.errors.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
tests/cases/conformance/nonjsExtensions/main.ts(1,18): error TS2307: Cannot find module './file.js' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(2,18): error TS2307: Cannot find module './file.jsx' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(3,18): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './file.js' instead.
tests/cases/conformance/nonjsExtensions/main.ts(4,18): error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './file.js' instead.
tests/cases/conformance/nonjsExtensions/main.ts(3,18): error TS2307: Cannot find module './file.ts' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(4,18): error TS2307: Cannot find module './file.tsx' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(5,18): error TS2307: Cannot find module './file.mjs' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(6,18): error TS2307: Cannot find module './file.cjs' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(7,18): error TS2691: An import path cannot end with a '.mts' extension. Consider importing './file.mjs' instead.
tests/cases/conformance/nonjsExtensions/main.ts(8,18): error TS2691: An import path cannot end with a '.cts' extension. Consider importing './file.cjs' instead.
tests/cases/conformance/nonjsExtensions/main.ts(9,18): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './file.js' instead.
tests/cases/conformance/nonjsExtensions/main.ts(10,19): error TS2691: An import path cannot end with a '.d.cts' extension. Consider importing './file.js' instead.
tests/cases/conformance/nonjsExtensions/main.ts(11,19): error TS2691: An import path cannot end with a '.d.mts' extension. Consider importing './file.js' instead.
tests/cases/conformance/nonjsExtensions/main.ts(12,19): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './file.d.json.js' instead.
tests/cases/conformance/nonjsExtensions/main.ts(7,18): error TS2307: Cannot find module './file.mts' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(8,18): error TS2307: Cannot find module './file.cts' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(9,18): error TS2307: Cannot find module './file.d.ts' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(10,19): error TS2307: Cannot find module './file.d.cts' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(11,19): error TS2307: Cannot find module './file.d.mts' or its corresponding type declarations.
tests/cases/conformance/nonjsExtensions/main.ts(12,19): error TS2307: Cannot find module './file.d.json.ts' or its corresponding type declarations.


==== tests/cases/conformance/nonjsExtensions/package.json (0 errors) ====
Expand All @@ -23,10 +23,10 @@ tests/cases/conformance/nonjsExtensions/main.ts(12,19): error TS2691: An import
!!! error TS2307: Cannot find module './file.jsx' or its corresponding type declarations.
import def3 from "./file.ts";
~~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.ts' extension. Consider importing './file.js' instead.
!!! error TS2307: Cannot find module './file.ts' or its corresponding type declarations.
import def4 from "./file.tsx";
~~~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './file.js' instead.
!!! error TS2307: Cannot find module './file.tsx' or its corresponding type declarations.
import def5 from "./file.mjs";
~~~~~~~~~~~~
!!! error TS2307: Cannot find module './file.mjs' or its corresponding type declarations.
Expand All @@ -35,22 +35,22 @@ tests/cases/conformance/nonjsExtensions/main.ts(12,19): error TS2691: An import
!!! error TS2307: Cannot find module './file.cjs' or its corresponding type declarations.
import def7 from "./file.mts";
~~~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.mts' extension. Consider importing './file.mjs' instead.
!!! error TS2307: Cannot find module './file.mts' or its corresponding type declarations.
import def8 from "./file.cts";
~~~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.cts' extension. Consider importing './file.cjs' instead.
!!! error TS2307: Cannot find module './file.cts' or its corresponding type declarations.
import def9 from "./file.d.ts";
~~~~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './file.js' instead.
!!! error TS2307: Cannot find module './file.d.ts' or its corresponding type declarations.
import def10 from "./file.d.cts";
~~~~~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.d.cts' extension. Consider importing './file.js' instead.
!!! error TS2307: Cannot find module './file.d.cts' or its corresponding type declarations.
import def11 from "./file.d.mts";
~~~~~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.d.mts' extension. Consider importing './file.js' instead.
!!! error TS2307: Cannot find module './file.d.mts' or its corresponding type declarations.
import def12 from "./file.d.json.ts";
~~~~~~~~~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.ts' extension. Consider importing './file.d.json.js' instead.
!!! error TS2307: Cannot find module './file.d.json.ts' or its corresponding type declarations.
==== tests/cases/conformance/nonjsExtensions/file.d.js.ts (0 errors) ====
declare var bad: "bad1";
export default bad;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,6 @@ Info 32 [00:01:13.000] response:
"2685",
"2687",
"2688",
"2691",
"2692",
"2694",
"2695",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ import type {} from "./tsx.d.ts";
import {} from "./dts.js";
import {} from "./dts.ts";
import type {} from "./dts.d.ts";

// @Filename: /c.ts
import {} from "./thisfiledoesnotexist.ts";