pnpmpnpm-workspace

Ambient declarations work, but `declare global` is lost in a monorepo workspace using pnpm


When I import a function from another package, it loses its type definition. The type is defined globally within that package. Other types work fine. How can I solve it?

It is necessary to say that I am just performing transition from yarn 1.22 to pnpm and this has worked with yarn just a commit prior. (see screenshot at the bottom)

pnpm-workspace.yaml
packages:
  - apps/*
  - packages/*
root package.json
  "pnpm": {
    "overrides": {
      "holograph": "link:packages/holograph",
    }
  },
app-v2 tsconfig.json
  "include": [
    "next-env.d.ts",
    "src/**/*.ts",
    "src/**/*.tsx",
    "../../packages/**/*.ts",
    "../../packages/**/*.tsx",
    "../../packages/next-config/next.global.d.ts",
  ],

Where apps/app-v2 (on left) imports from packages/holograph (on right).


Furthermore, I noticed that declaring the type in an Ambient declarations file (interfaces.d.ts) works, but it is not picked up when contributing ambient declarations via declare global keyword.


After reverting the commit back and installing with yarn, the type is working again.

Also in pnpm repo


Solution

  • When exploring differences between the yarn and pnpm workspaces I noticed that the holograph package is symlinked from the root node_modules in the yarn workspace, but it was only linked in the apps/app-v2/node_modules in the pnpm workspace. I suppose this is something called or related to "package hoisting" and the way I fixed it is that I manually specified the packages that contribute types as a reference in the root package.json which seems to be the source of truth for tooling and IDE.

    So the working state looks like this:

    apps/app-v2/package.json

      "dependencies": {
        "holograph": "*",
      },
    

    And I added the same in the root package.json

      "devDependencies": {
        "holograph": "*",
      },
    

    The result is that both node_modules now link the holograph package.

    ll node_modules/ | grep holo
    lrwxrwxrwx    1 qwerty qwerty     21 Jul  5 00:15 holograph -> ../packages/holograph/
    
    ll apps/app-v2/node_modules/ | grep holo
    lrwxrwxrwx 1 qwerty qwerty   27 Jul  5 00:15 holograph -> ../../../packages/holograph/
    

    And the types are picked up correctly.