-
Notifications
You must be signed in to change notification settings - Fork 13.1k
feat: adjust TS2691 message for .ts import sources #42184
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
Conversation
andrewbranch
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test that shows this error? Thanks!
|
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 |
|
|
Don’t be! Contributions are always appreciated and we expect first-time contributors to have some questions 👍 |
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested change for CJS module target.
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested change for ES module target.
andrewbranch
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ctjlewis!
* Adjust TS2691 message for .ts import sources * Only ModuleKind is needed for TS2691 logic * Added tests for TS2691
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.