feat: adjust TS2691 message for .ts import sources#42184
Merged
andrewbranch merged 3 commits intomicrosoft:masterfrom Jan 5, 2021
Merged
feat: adjust TS2691 message for .ts import sources#42184andrewbranch merged 3 commits intomicrosoft:masterfrom
andrewbranch merged 3 commits intomicrosoft:masterfrom
Conversation
andrewbranch
requested changes
Jan 4, 2021
Member
andrewbranch
left a comment
There was a problem hiding this comment.
Can you add a test that shows this error? Thanks!
Contributor
Author
|
Sure @andrewbranch - if you see this in time, is there a way to run just a single test in this workspace, rather than running the whole suite with |
Member
|
|
Member
Don’t be! Contributions are always appreciated and we expect first-time contributors to have some questions 👍 |
ctjlewis
commented
Jan 5, 2021
Comment on lines
+1
to
+3
| tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x' instead. | ||
| tests/cases/compiler/user.ts(2,15): error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y' instead. | ||
| tests/cases/compiler/user.ts(3,15): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead. |
Contributor
Author
There was a problem hiding this comment.
Suggested change for CJS module target.
ctjlewis
commented
Jan 5, 2021
Comment on lines
+1
to
+3
| tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './x.js' instead. | ||
| tests/cases/compiler/user.ts(2,15): error TS2691: An import path cannot end with a '.tsx' extension. Consider importing './y.js' instead. | ||
| tests/cases/compiler/user.ts(3,15): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z.js' instead. |
Contributor
Author
There was a problem hiding this comment.
Suggested change for ES module target.
weswigham
approved these changes
Jan 5, 2021
Zzzen
pushed a commit
to Zzzen/TypeScript
that referenced
this pull request
Jan 16, 2021
* Adjust TS2691 message for .ts import sources * Only ModuleKind is needed for TS2691 logic * Added tests for TS2691
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The advice given by error
TS2691is confusing, and following its instructions produces output which throwsERR_MODULE_NOT_FOUNDat runtime when outputting an ES module, i.e. whenmoduleKind >= ModuleKind.ES2015.An import for a module located at
./test.tsmust be specified asimport ... from './test.js'in order to get a valid ES module out, but users are directed to rename it to./test. This error message should be updated so users are given the correct advice based on their use case, and are not confused why their output throws despite the source being perfectly valid TS. (The./testformat can be used to generate valid CommonJS modules, just not ES modules.)Closes #42151.
Current behavior
Currently throws
TS2691which says:This adjustment does silence the compiler, but the compiled JS looks like:
Which is not a valid ES module, as the import source is located at
./test.js. Executing the output throwsERR_MODULE_NOT_FOUND.Suggested changes
This PR adjusts
checker.tsto detect if the output is an ES module, and if so advises the user to replace./import.tswith./import.jsinstead of./import.After these changes, trying to import
./test.jswhile outputting an ES module throws:Which will compile to a valid ES module that behaves as expected.