Description
🔍 Search Terms
"rewriteRelativeImportExtensions", "paths", "ts 2877"
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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.