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 X cannot be named without a reference to Y" (TS2742) occurs when multiple modules with the same package ID are resolved #47663

Open
renke opened this issue Jan 30, 2022 · 18 comments
Labels
Needs Investigation This issue needs a team member to investigate its status.
Milestone

Comments

@renke
Copy link

renke commented Jan 30, 2022

Bug Report

🔎 Search Terms

  • TS2742
  • cannot be named without a reference

🕗 Version & Regression Information

Problems occurs with 4.5.x and 4.6.x and most likely earlier versions (I've tried a few other versions). It stills occurs on 4.7.0-dev.20220321.

⏄1�7 Playground Link

I've created minimal repository that shows the problem: https://github.com/renke/typescript-package-id-merge-repro

The problem occurs when using pnpm (due to the modules layout it uses). No problems occur when using npm/yarn.

To reproduce the problem run pnpm install and then pnpm check pnpx tsc -b.

💻 Code

I don't think the code itself matters to much except from the fact that it in fact does not have explicit type annotations (which is kind of the idea when using zod and by extension @renke/vommer).

import { vod } from "@renke/vommer";
import { z } from "zod";

export const UserId = vod("UserId", z.string());

export type UserId = z.infer<typeof UserId>;

export const Email = vod("Email", z.string().min(0));

export type Email = z.infer<typeof Email>;

export const User = vod(
  "User",
  z.object({
    id: UserId,
    email: Email,
  })
);

export type User = z.infer<typeof User>;

🙁 Actual behavior

The following error occurs when trying to build a composite TypeScript project (same happens when just using declaration: true).

The inferred type of 'User' cannot be named without a reference to '.pnpm/@renke+vo@0.2.0/node_modules/@renke/vo'. This is likely not portable. A type annotation is necessary.

The dependency tree of @renke/vommer

@renke/vommer 0.2.0
├─┄1�7 @renke/vo 0.2.0
└─┄1�7 @renke/vod 0.2.0
  └─┄1�7 @renke/vo 0.2.0

Looking at the resolution trace TypeScript tries to resolve @renke/vo two times the first time from @renke/vommer and the second time from @renke/vod. Both end up having the package ID @renke/vo/dist/index.d.ts@0.2.0.

Using "preserveSymlinks": true doesn't solve the problem in so far that the error disappears but the type is inferred as any, because the dependencies of @renke/vommer are not found. Also I don't actually want to use it.

🙂 Expected behavior

The error should not occur when there are two (or more) modules that have the same resolved package ID. It would make sense for the error to occur when they have different versions.

@zengguirong
Copy link

zengguirong commented Feb 9, 2022

same problem

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 10, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Feb 10, 2022
@renke
Copy link
Author

renke commented Mar 16, 2022

So, aside from finding a solution to this problem, is my assumption correct that having multiple packages with typings that have the same version should not cause any problems?

I'd also appreciate any hints on where to look at the source code to solve this problem.

@dohooo
Copy link

dohooo commented Apr 24, 2022

same problem!

@quadristan
Copy link

quadristan commented May 23, 2022

@renke i have a repro where everything has the same version here : https://github.com/quadristan/ts-indirect-type-reference-bug

@agmitron
Copy link

agmitron commented Jul 10, 2022

same problem :( have you solved it?

@renke
Copy link
Author

renke commented Jul 15, 2022

I haven't solved it yet, but there is also a similar issue #48212 with a milestone of TypeScript 4.8.0. Let's hope it will be solved soon.

@StarHosea
Copy link

StarHosea commented Aug 8, 2022

same problem

@kiastorm
Copy link

kiastorm commented Aug 24, 2022

I got this issue because the mentioned dependency had this code in its index.d.ts:

declare module 'react' {
  interface DOMAttributes<T> {
    css?: InterpolationWithTheme<any>
  }
}

and the file it was complaining in was using React.HTMLAttributes<HTMLElement> which references React.DOMAttributes. I worked around this issue by omitting the declared properties Omit<React.HTMLAttribute<HTMLElement>, "css">

@LeandroFontellas
Copy link

LeandroFontellas commented Aug 26, 2022

Same problem :(

@vaibhavkumar-sf
Copy link

vaibhavkumar-sf commented Sep 2, 2022

Same Problem for me too :(

@ivanbanov
Copy link

ivanbanov commented Sep 12, 2022

is there any expected timeline for a fix for this problem?

@grmkris
Copy link

grmkris commented Sep 15, 2022

same here

@patroza
Copy link

patroza commented Sep 16, 2022

@RyanCavanaugh

@yingpengsha
Copy link

yingpengsha commented Sep 21, 2022

Same problem :(

@Evertt
Copy link

Evertt commented Sep 26, 2022

same here

1 similar comment
@xlboy
Copy link

xlboy commented Sep 28, 2022

same here

@shellscape
Copy link

shellscape commented Sep 30, 2022

What's worse is that neither // @ts-ignore nor // @ts-expect-error will allow us to ignore the incorrect error. This is a pretty gnarly bug.

@mrmeku
Copy link

mrmeku commented Oct 4, 2022

This bug occurs pretty readily when using pnpm since the node_modules directory layout is different under pnpm than npm/yarn. Any npm package whose types reference another npm package's type will generate the error

I've made a minimal reproduction here: https://github.com/mrmeku/portable_types_repro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests