Search code examples
typescriptlernamonorepo

Using @types from specific folders with typescript


We have a monorepo using Lerna, with package a and package b both containing @types/react.

Package A has Package B as a dependent so I end up with this:

Package A: node_modules/PackageB/node_modules/@types

Which causes loads of duplicate errors.

We don't use any workspaces (as of yet) and we use NPM. When compiling with TSC (for type checking), it throws a load of errors about duplicate packages and missing packages.

I was hoping there might be a way with tsconfig to specify the types folder I want to use? So instead of just grabbing all of them, it grabs only node_modules/@types rather than nested ones?

I've tried using typesRoot, types, and paths. All to no avail. Currently we're using skipLibCheck, but it's no ideal.


Solution

  • Each package should have a dependency on react types (don't use file paths!), lerna bootstrap used with the --hoist flag will wire them up without duplication. You will probably want to also use the --strict flag which will ensure you keep the dependency versions in lockstep.