Skip to content

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

Closed
@jameshfisher

Description

@jameshfisher

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

Metadata

Metadata

Assignees

Labels

DocsThe issue relates to how you learn TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions