-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Rewrite relative import extensions with flag #59767
Rewrite relative import extensions with flag #59767
Conversation
@typescript-bot pack this |
Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
7159132
to
2b06e62
Compare
Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page. Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up. |
bf25d52
to
a398ad1
Compare
ef2101a
to
0d95ce2
Compare
The original issue was locked; it would be nice for someone with permissions to comment there so that people who were following there get this update. |
@RyanCavanaugh I happened upon your final post here: #49083 (comment) minutes before you commented and referenced this PR. I'm curious what changed from your reasoning a year ago as to why this would never be a feature? |
NodeJS shipping |
Note that if you're using this in any other situation than that, you likely have a misconfiguration or misunderstanding of some kind, and I'll be telling you as much in a future issue when you report that e.g. this "doesn't work" across package boundaries. |
I have tested TS It does not rewrite My library preset on /* Library preset */
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noEmit": false,
"declaration": true, and new one on /* Library preset */
"module": "NodeNext",
"moduleResolution": "NodeNext",
"allowImportingTsExtensions": true, // <- new
"rewriteRelativeImportExtensions": true, // <- new
"noEmit": false,
"declaration": true, It works, I can use Also, now I can run TS files with However, imports inside Or is it OK? (I didn't check if there would be Anyway, I think that both configs should produce absolutely the same result ( |
Imports in |
It's a bug. Non-rewritten imports in It's OK:
After using
In this case, the IDE no longer highlights the function names, nor does it show the function signature. The imports in Because all Because |
Also, it currently (?) does not support aliases which resolve to a relative path. {
"extends": "../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["../*"]
}
}
} I get the follow error for
|
This is 100% intentional |
Is it a problem to resolve the alias first and only after that check if it is a relative path or not? It doesn't seem like something that is difficult to implement. Just only one extra simple action. |
I don't think the setup you have there works under |
I can't replicate what you're doing there // with
// import * as bar from "./bar.ts";
node --experimental-strip-types foo.ts
hi vs // with
// import * as bar from "@/bar.ts";
Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@/bar.ts' imported from D:\Throwaway\est-test\foo.ts The purpose of the flag is to rewrite paths which are syntactically relative, not "semantically" relative (whatever that would even mean). |
Hey, I was reading the table in the PR description and I'm getting confused by these two lines:
Does it mean that |
Yes. Plain |
## Summary <!-- Succinctly describe your change, providing context, what you've changed, and why. --> Ensures support for `typescript@~5.7.0` by adding a new PR check. See [Announcing TypeScript 5.7 - TypeScript](https://devblogs.microsoft.com/typescript/announcing-typescript-5-7/). Ideally we would take advantage of the new `rewriteRelativeImportExtensions` added in microsoft/TypeScript#59767 which would allow us to correctly import `.ts` extensions instead of requiring `.js`, ready for better Deno/Bun support, as well as Node support when running `.ts` files directly. Unfortunately, `typescript<5.0.0` doesn't support the compiled output; we'll have to wait for v4 to use that. ## Checklist <!-- Tick these items off as you progress. --> <!-- If an item isn't applicable, ideally please strikeout the item by wrapping it in "~~"" and suffix it with "N/A My reason for skipping this." --> <!-- e.g. "- [ ] ~~Added tests~~ N/A Only touches docs" --> - [ ] ~Added a [docs PR](https://github.com/inngest/website) that references this PR~ N/A KTLO - [x] Added unit/integration tests - [x] Added changesets if applicable ## Related - https://devblogs.microsoft.com/typescript/announcing-typescript-5-7/ - microsoft/TypeScript#59767
This PR adds
--rewriteRelativeImportExtensions
to support transforming module specifiers from e.g."./utils.ts"
to"./utils.js"
. A module specifier will be statically rewritten or shimmed during JS emit if it meets all of these criteria:./
or../
.d.ts
,.d.mts
,.d.cts
,.d.*.ts
).ts
,.tsx
,.mts
, or.cts
Error checking
Errors are issued in an attempt to catch common mistakes:
"./foo.ts"
actually resolves to./foo.ts.ts
orfoo.ts/index.ts
"../other-project/src/index.ts"
belongs to another project where outputs go to a different outDir"#blah/foo.ts"
resolves tofoo.ts
where the.ts
matches through a wildcardSyntax support
import "./foo.ts"
import "./foo.js"
,require("./foo.js")
export * from "./foo.ts"
export * from "./foo.js"
import foo = require("./foo.ts")
import foo = require("./foo.js")
import("./foo.ts")
import("./foo.js")
require("./foo.ts")
require("./foo.js")
import(getPath())
import(__rewriteRelativeImportExtension(getPath()))
require(getPath())
require(__rewriteRelativeImportExtension(getPath()))
import("foo")
import("foo")
require("foo")
require("foo")
require("./foo.ts")
require("./foo.ts")
(require)("./foo.ts")
(require)("./foo.ts")
Limitations and future work
This PR doesn’t change how module resolution works, so it’s still possible to reference a
.ts
file by a.js
module specifier:We discussed that it’s probably desirable to turn this functionality off under
--rewriteRelativeImportExtensions
(and--allowImportingTsExtensions
), since these options are a strong indicator that the user is going to run the input TS files in a TS-native runtime like Bun ornode --experimental-strip-types
. That would be a breaking change and isn’t included in this PR.tslib PR: microsoft/tslib#270
Fixes #59597