typescriptnpm-workspaces

NPM Workspaces: Share `*.d.ts` TypeScript definition files between workspaces


I have a TypeScript monorepo with NPM Workspaces.

There are two packages; A and B.

I need type definitions from A inside B.

I have added reference to A's definition file in package B's tsconfig.json. However it doesn't feel right:

{
  "include": ["../a/types.d.ts", "next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

It's the relative reference I don't like. I expected it to work with a/types.d.ts as "a" is the package name — but it doesn't.

Am I doing anything wrong?


Solution

  • I found the answer to this to be to create a package for the config files. This is also the way Turborepo does it, when instantiating a new monorepo. Here they add @repo/typescript-config and @repo/eslint-config packages. I believe you should add @repo/type-definitions like that.