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

Guidance needed: how to configure tooling to use .js file extension in TypeScript imports #52412

Closed
jameshfisher opened this issue Jan 25, 2023 · 9 comments
Assignees
Labels
Docs The issue relates to how you learn TypeScript

Comments

@jameshfisher
Copy link

Suggestion

It is TypeScript's policy (see #40878) that, if you want tsc --build to produce working JavaScript, the import statements in your TypeScript files must have .js extensions. In @RyanCavanaugh's words: "you should always write the path you want to appear in the emitted JS, and configure the project as necessary to make those paths resolve the way you want during compilation".

There is a long and painful discussion of whether that policy is sensible -- I do not wish to continue that discussion here. What is needed, though, is guidance on how to "configure the project as necessary" to include the .js extension. For example:

  • How can we configure VS Code (or any other IDE) to use a .js extension when adding an import statement?
  • How can we configure linting tools to warn about extensionless imports, so we don't accidentally commit them?
  • How can we automatically fix existing broken codebases that contain extensionless imports?
  • How can we configure TypeScript to emit a compile error if encountering an extensionless import?

Despite opening #40878 over 2 years ago, I still don't know the answers to these questions. Judging from other responses, I think a lot of other people are in the same position. @RyanCavanaugh never went into detail on this, and I can't find any guidance in the TypeScript docs. So - people in the know, can you share how you have configured your tools to work around this TypeScript policy?

🔍 Search Terms

js extension tooling

@fatcerberus
Copy link

fatcerberus commented Jan 25, 2023

How can we configure TypeScript to emit a compile error if encountering an extensionless import?

This is already enforced for moduleResolution: "node16" and "nodenext". If you're targeting something other than Node, see #52204 and #50153.

How can we configure VS Code ... to use a .js extension when adding an import statement?

"typescript.preferences.importModuleSpecifierEnding": "js"

@jameshfisher
Copy link
Author

@fatcerberus Ooh, those are both extremely useful, thank you!

Is there a way to get VS Code to "quick fix" all broken extensionless imports? Or some other one-time script for that? Or should I do some hacky regex?

@jameshfisher
Copy link
Author

@fatcerberus this typescript.preferences.importModuleSpecifierEnding preference seems to work for "auto adding" imports, but I've just found a different situation that it doesn't work with: When I move a file, it updates relative imports, but in the process it removes all of my (manually added!) .js extensions. Are there separate preferences for different situations like this? (I couldn't find documentation for typescript.preferences.importModuleSpecifierEnding)

@fatcerberus
Copy link

Yikes. Looks like there might be a bug or two with that setting: #46290

@RyanCavanaugh
Copy link
Member

We're working on a War-and-Peace-length set of documentation around the module area that will address these questions and many more

@RyanCavanaugh RyanCavanaugh added the Docs The issue relates to how you learn TypeScript label Jan 25, 2023
@andrewbranch
Copy link
Member

It is TypeScript's policy (see #40878) that, if you want tsc --build to produce working JavaScript, the import statements in your TypeScript files must have .js extensions.

Not quite; it’s just that your import statements on the input side need to look like you want them to look on the output side. Extensionless imports work in many places, but not all. Where are you running the JS files emitted by tsc?

@jameshfisher
Copy link
Author

Hopefully the new documentation could show how to configure a project to robustly work with fragmented package ecosystem. I haven't been able to find a configuration that lets me just use packages on npm. Latest example with moduleResolution:

@jmrossy
Copy link

jmrossy commented Feb 3, 2023

We're working on a War-and-Peace-length set of documentation around the module area that will address these questions and many more

Genuinely looking forward to having all the disparate bits of knowledge about modules gathered in one place, thanks! How would I get notified when that's available?

@andrewbranch
Copy link
Member

@jmrossy I opened #52593 that you can subscribe to. I’m going to close this one under that umbrella.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants