How to move global reusable types to packages type in turborepo?

207 views Asked by At

For any app in apps, I need a global.d.ts having some types like this

interface Window{
    analytics: any;
}

It needs to be reusable and shifted to packages/types and can be used anywhere in apps.

I moved the Window type to packages/types but global types weren't working while importing in apps.

1

There are 1 answers

0
Yes Dev On

Setting this up properly requires a few steps:

  1. Create a shared-types package inside the packages folder of your monorepo
  2. In the package.json of shared-types, define where the types are located:
{
    "name": "shared-types",
    "version": "1.0.0",
    "main": "./index.js",
    "types": "./index.ts",
    "dependencies": {
        "typescript": "latest"
      }
}
  1. Add the shared-types dependency to the apps that need it
 "dependencies": {
    ...
    "shared-types": "*"
  },
  1. Import the types you need
import { MyType } from "shared-types";

See this Sharing Code and Internal Packages sections of the Turborepo docs for more details.