I've been looking around the web to try and find s nice solution to my problem but so far I have not.
I have a NX monorepo with NextJS and I am trying to create a global types/
folder that can be used by all of my apps and libs.
I had thought this would be quite simple, I would add a folder to the root and put all my .d.ts
files in there. Then in the tsconfig.base.json I would add
"typeRoots": [
"node_modules/@types",
"node_modules/next/types",
"types/"
]
I was hoping it would be as simple as that.
Allowing me to use my typing anywhere within the monorepo without having to import them.
Unfortunately, this didn't work. After lots of messing around I did manage to get it to work but only if the types/
folder was inside of the app, for example:
apps/
/myApp/
/types/ <- types here were detected
types/ <- types here were not detected
tsconfig.base.json
This is no good for me as it would mean I would have to duplicate my types across all apps and libs.
The official recommendation from NX is to add a new lib just to store your types and then import it and use it in each of your apps and libs but this feels really clunky to me. Also, this doesn't really work with the idea of adding it to typeRoots
within the tsbase.config.json
If anyone knows a better way this can be done I would love to hear about it.
Cheers.
Battling with the same issue right now. So far, this works for my project, but it's still not an ideal solution. I would much rather have a global provider.
apps/
/myApp/
tsconfig.app.json
types/ <- add your .d.ts files here
tsconfig.base.json
in the tsconfig.app.json
of each app add the following:
"include": ["../../types/**/*.d.ts"]
This will import all the type definitions stored in the global folder. But the configuration still has to be handled manually in each library and app.
Let me know if this is helpful or if you have found a better solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With