-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
RR7 typegen doesn't support moduleResolution node16 #12424
Comments
We require Node 20 or higher, so this behavior is expected. However, we should ensure |
Hey, thanks for quick response. :)
I don't think I understand this part. What's with the Node.js version? This same behavior appears both with the Node version from Stackblitz and the one specified in the issue (it is the same version from my test project with RR7). |
I had the same problem, except I'm using diff --git a/node_modules/@react-router/dev/dist/cli/index.js b/node_modules/@react-router/dev/dist/cli/index.js
index 266c57f..660e362 100644
--- a/node_modules/@react-router/dev/dist/cli/index.js
+++ b/node_modules/@react-router/dev/dist/cli/index.js
@@ -600,7 +600,7 @@ function generate(ctx, route) {
const indent = i === 0 ? "" : " ".repeat(2);
let source = noExtension(rel);
if (!source.startsWith("../")) source = "./" + source;
- return `${indent}import type { Info as Parent${i} } from "${source}"`;
+ return `${indent}import type { Info as Parent${i} } from "${source}.js"`;
}).join("\n");
return import_dedent.default`
// React Router generated types for route:
@@ -610,7 +610,7 @@ function generate(ctx, route) {
${parentTypeImports}
- type Module = typeof import("../${Pathe2.filename(route.file)}")
+ type Module = typeof import("../${Pathe2.filename(route.file)}.js")
export type Info = {
parents: [${parents.map((_, i) => `Parent${i}`).join(", ")}],
diff --git a/node_modules/@react-router/dev/dist/vite.js b/node_modules/@react-router/dev/dist/vite.js
index f17d1b6..abff485 100644
--- a/node_modules/@react-router/dev/dist/vite.js
+++ b/node_modules/@react-router/dev/dist/vite.js
@@ -641,7 +641,7 @@ function generate(ctx, route) {
const indent = i === 0 ? "" : " ".repeat(2);
let source = noExtension(rel);
if (!source.startsWith("../")) source = "./" + source;
- return `${indent}import type { Info as Parent${i} } from "${source}"`;
+ return `${indent}import type { Info as Parent${i} } from "${source}.js"`;
}).join("\n");
return import_dedent.default`
// React Router generated types for route:
@@ -651,7 +651,7 @@ function generate(ctx, route) {
${parentTypeImports}
- type Module = typeof import("../${Pathe2.filename(route.file)}")
+ type Module = typeof import("../${Pathe2.filename(route.file)}.js")
export type Info = {
parents: [${parents.map((_, i) => `Parent${i}`).join(", ")}], |
You may also need this so type augmentation of diff --git a/node_modules/react-router/package.json b/node_modules/react-router/package.json
index 1fbd582..373a206 100644
--- a/node_modules/react-router/package.json
+++ b/node_modules/react-router/package.json
@@ -24,7 +24,7 @@
"exports": {
".": {
"node": {
- "types": "./dist/production/index.d.ts",
+ "types": "./dist/production/index.d.mts",
"development": {
"module-sync": "./dist/development/index.mjs",
"default": "./dist/development/index.js" Without this, the |
Yes, your patch does the trick. Thank you! Yet it still need to be fixed in RR itself. |
Using React Router as a framework means using Vite as a bundler. In this case, I think setting What reasons do you have for preferring |
Ok quick update: chatted with @gustavopch offline and learned that |
Hey, glad to hear your thoughts!
In my case I usually use RR with Mikro ORM, which has a CLI to manage stuff like seeding, migrations etc. This CLI supports TypeScript code via ts-node, which does not do any transformations for paths for module imports, so Node.js will have same paths as the input file. Because of this, I'll get an error trying to run ESM code if my paths have no file extension at the end. I can add extensions where needed, but then I either end up writing them everywhere or just for server code that will be run by the CLI. So, I have to remember to add extension myself every time or I'll get inconsistency in my code. So, yes, |
🤖 Hello there, We just published version Thanks! |
🤖 Hello there, We just published version Thanks! |
🤖 Hello there, We just published version Thanks! |
🤖 Hello there, We just published version Thanks! |
🤖 Hello there, We just published version Thanks! |
🤖 Hello there, We just published version Thanks! |
I'm using React Router as a...
framework
Reproduction
https://stackblitz.com/edit/rr7-typegen-node16-support-issue?file=app%2Froutes%2Fhello.%24name.tsx
Just open the
app/routes/hello.$name.tsx
file and look at theloaderData
type. You'll see it is correct. Then opentsconfig.json
and setmoduleResolution
tonode16
, and then return to previous file. NowloaderData
is undefined.System Info
Used Package Manager
pnpm
Expected Behavior
loaderData has correct types when moduleResolution is set to node16. In this case - it should be a string.
This might not be an issue for the most (I think), but for me - it is, because this is how Node.js resolves ES modules by default and this might be a problem when I have a code that is shared between Vite and Node.js, so I expect this to work.
Actual Behavior
loaderData is of type undefined when moduleResolution is set to node16
This happens because TS expects files to have a
.js
at the end of module specifier, same way as Node.js does. From what I can tell, the paths generated by RR's Vite plugin for parent routes are incorrect. This code always outputs path without extension:react-router/packages/react-router-dev/typegen/generate.ts
Line 37 in 72f9af9
So, I think to solve this problem, it should respect the
moduleResolution
option fromtsconfig.json
(andallowImportingTsExtensions
to output.ts
at the end of the specifier, if enabled).The text was updated successfully, but these errors were encountered: