Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The inferred type of '...' cannot be named without a reference to '../../lib-b/node_modules/...'. This is likely not portable. #6089

Open
GuillaumeLagrange opened this issue Feb 16, 2023 · 5 comments
Assignees

Comments

@GuillaumeLagrange
Copy link

GuillaumeLagrange commented Feb 16, 2023

I have an issue in my monorepo using pnpm and typescript, which started to occur after my migration from yarn to pnpm.

Here is the gist of how to reproduce the issue

  • Package @my-app/lib-b package has random-package (not important) as dependency, exporting a TypeFromDependency type
  • In this package, define a function sharedFunction whose return type depends directly on TypeFromDependency
  • Service @my-app/lib-a has @my-app/lib-a as dependency and uses sharedFunction as follows:
export const bar = sharedFunction();

This produces the following warning

The inferred type of 'bar' cannot be named without a reference to '../../lib-b/node_modules/random-package/dist'. This is likely not portable. A type annotation is necessary.

pnpm version:

7.27.0

Code to reproduce the issue:

https://github.com/GuillaumeLagrange/type-issue

  • Clone the repo
  • Run pnpm install
  • Run pnpm package (the command will fail because tsc fails in libA workspace, this is the issue)
  • Open packages/lib-a/src/libA.ts in VSCode/your IDE, see the following error
The inferred type of 'exportedLibB' cannot be named without a reference to '../../lib-b/node_modules/random/dist'. This is likely not portable. A type annotation is necessary.

Expected behavior:

Unless I am mistaken, no type error should occur, lib-a exports a variable which is the return value of a lib-b function, declared as dependency

Actual behavior:

The inferred type of 'exportedLibB' cannot be named without a reference to '../../lib-b/node_modules/random/dist'. This is likely not portable. A type annotation is necessary.

Additional information:

  • node -v prints: v16.19.0
  • Windows, macOS, or Linux?: Reproduced on MacOS and Linux, no windows computer to test this out.

Notes

The issue occurs with
$ pnpm tsc in packages/lib-a
But does not occur when running
$ pnpm tsc --noEmit in packages/lib-a

It looks like microsoft/TypeScript#47663

I have originally opened the issue in here swarmion/swarmion#508

@await-ovo
Copy link
Member

According this comment microsoft/TypeScript#42873 (comment), seems that we can use this as a workaround:

image

@GuillaumeLagrange
Copy link
Author

It does seem like the easiest workaround, just tested on my project and it works. Thank you for pointing it out !

I think the issue is still worth keeping open until the root cause is fixed, but I will let maintainers here be the judge of that

@jgoux
Copy link

jgoux commented Apr 8, 2023

The workaround doesn't work when you export a type which is then consumed by the other package.

In my case:

// packages/api
export type WebRouter = typeof webRouter
// packages/web
import { createReactQueryHooks } from '@trpc/react'
import type { WebRouter } from 'api'
const trpc = createReactQueryHooks<WebRouter>()
web/src/lib/trpc.tsx:7:14 - error TS2742: The inferred type of 'trpc' cannot be named without a reference to '../../../api/node_modules/@snaplet/sdk/src'. This is likely not portable. A type annotation is necessary.

7 export const trpc = createReactQueryHooks<WebRouter>()

I had the same issue involving @prisma/client and @trpc/server, I was able to resolve them by adding both to public hoisting and manually deleting their local symlink in packages/api/node_modules/{@prisma/client,@trpc/server}.

The main issue now is that web being one of my own packages in my workspace, I have no option to hoist it to the root with pnpm.

I wish it could be solved more simply.

@pkerschbaum
Copy link

FYI I added a comment in TypeScript#47663 which has workarounds for this issue when using pnpm monorepos.

@aminya
Copy link

aminya commented Apr 15, 2024

Using node-linker=hoisted fixes the issue. However, it causes the post-install scripts to run every time you run the install. But it is worth fixing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants