Skip to content

rewriteRelativeImportExtensions & enforce consistent extensionsΒ #61021

Open
@darksabrefr

Description

@darksabrefr

πŸ” Search Terms

"rewriteRelativeImportExtensions", "extensions"

βœ… Viability Checklist

⭐ Suggestion

An option/param to enforce imports with relative paths to point to files existing in sources rather than in built files, in short, make imports only allowing .ts and not .js extensions when importing a Typescript file.

πŸ“ƒ Motivating Example

rewriteRelativeImportExtensions is a great new feature and it works well. It permits to create hybrid projects directly executed by recent nodejs versions, but also buildable by tsc for distributing them. But since we now have the possibility to use the .ts extensions in imports because they will be rewritten at compilation time, we still can continue to use the .js extensions in parallel, pointing to built files.
As everyone knows, nodejs requires relative imports to have an extension and to point to an existing file and so, extensions have to be .ts (or .mts/.cts) to import other Typescript files.
It would be probably great that Typescript gives us an error when using unreachable file in this context, like its quasi-exact opposite: TS5097: An import path can only end with a .ts extension when allowImportingTsExtensions is enabled when .ts extensions are not allowed by configs.

πŸ’» Use Cases

  1. What do you want to use this for?
    In projects where nodejs can be used for any reason (tests, various scripts) and where code have to be built to be distributed over npm or any other repository, to avoid detecting bad imports extensions at nodejs runtime and to keep a consistent code base.

  2. What shortcomings exist with current approaches?
    Only its permittivity, allowing to use inconsistent imports styles, that may don't run in nodejs, without detecting them at coding time.

  3. What workarounds are you using in the meantime?
    It's not a bug, there's no need of workarounds, only vigilance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions