How to consume types generated by react-router
v7 in another package using the internal package pattern?
#13421
Replies: 2 comments 5 replies
-
I guess you later intend to consume these from the app itself? It can’t really work because you’d have cyclical dependency. If that’s not the case (but I’m not sure what you’d be doing then haha) you can just create a file in your app and export types you wish to export from here. Consumer shouldn’t be aware of how your tsconfig is setup |
Beta Was this translation helpful? Give feedback.
-
For anyone coming accross this discussion, I found a solution which works for me - it might not be the very best though. Since I am working with subpath imports anyway, I added one for "imports": {
"#react-router/*": "./.react-router/*",
"#app/*": "./app/*"
} I use type Module = typeof import("../root.js") They work inside import { readFileSync, writeFileSync } from 'node:fs'
import { $, chalk, glob, spinner } from 'zx'
await spinner(
chalk.gray(`Generating ${chalk.cyanBright('react-router')} types...`),
() => $`pnpm react-router typegen`
)
const GENERATED_PATH_REGEX = /(app(?:\/routes\/)?.*\/)\+types/
const MODULE_TYPE_REGEX = /type Module = typeof import\("(.*)"\)/
await spinner(
chalk.gray('Fixing relative paths in generated types...'),
async () => {
const generatedFilePaths = await glob('.react-router/**/*.ts')
for (const generatedFilePath of generatedFilePaths) {
const match = generatedFilePath.match(GENERATED_PATH_REGEX)
if (!match) continue
const [_, importPath] = match
const content = readFileSync(generatedFilePath, 'utf8')
writeFileSync(
generatedFilePath,
content.replace(MODULE_TYPE_REGEX, (_, modulePath: string) => {
return `type Module = typeof import("#${importPath}${modulePath.replace('../', '')}")`
}),
'utf8'
)
}
}
)
console.log(
`${chalk.greenBright(
'✓'
)} Successfully generated types for ${chalk.cyanBright('react-router')}`
) Whenever I generate the types using this script, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I am trying to work with the internal package pattern and
react-router
v7. I am building a SaaS application with a package atpackages/frontend-themes
including different themes/designs for different domains. Thereact-router
application lives atapps/frontend
. I want to export the type of actions or their return type from the frontend package so that I can access the returned data at various points in thefrontend-themes
package e.g. by usinguseActionData()
oruseFetcher()
.Example
Imagine I have an action in my
frontend
app like this:TypeScript Error
When I import
CheckoutAction
in thefrontend-themes
package, however, the TypeScript compiler complains during typechecking like this:What I've tried
I know that the generated types from
react-router
are configured to work via TypeScript'srootDirs
option. It seems like thefrontend-themes
package is not aware of this config. I already tried the following adjustments to itstsconfig.json
without success:Does anyone have an idea how I could make the internal package pattern work for my setup? I have a few other plans if it doesn't work but this approach would have the best DX. Thank you so much in advance!
Beta Was this translation helpful? Give feedback.
All reactions