Skip to content

Allow rewriteRelativeImportExtensions to be used with import paths aliases #61991

Closed as not planned
@adamhl8

Description

@adamhl8

🔍 Search Terms

"rewriteRelativeImportExtensions", "paths", "ts 2877"

✅ Viability Checklist

⭐ Suggestion

Say you have paths set up like so:

"paths": {
  "@/*": ["./src/*"]
}

When rewriteRelativeImportExtensions is true and you use an import alias, you get an error.

import { foo } from "@/my-module.ts"

This import uses a '.ts' extension to resolve to an input TypeScript file, but will not be rewritten during emit because it is not a relative path. (ts 2877)

See my comment here: #61991 (comment)

This error makes sense and I want to be clear about what I'm not suggesting:
- I'm not suggesting that this shouldn't be an error
- I'm not suggesting that TypeScript should rewrite paths aliases

However, there are use cases where the above can be valid. For example, you can use something like justkey007/tsc-alias to rewrite these import aliases after the fact. e.g. tsc && tsc-alias

I have a few ideas on how to allow this:
1. When "moduleResolution": "bundler", this error should not be shown. This seems like the easiest/best fix, although maybe there are situations where this would be bad.
2. Add an option like allowImportingTsExtensionsWIthPaths, but I'm not really a fan of adding a compiler option specifically for this.

Alternatively, maybe this error can just be removed altogether? paths aliases are not rewritten anyway, and tsc will happily compile something with a .js extension like import { foo } from "@/my-module.js", which will not work at runtime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions