"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
Description
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.
⏯ 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 checkpnpx 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/@[email protected]/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
├── @renke/vo 0.2.0
└─┬ @renke/vod 0.2.0
└── @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/[email protected].
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.
Activity
zengguirong commentedon Feb 9, 2022
same problem
renke commentedon 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 commentedon Apr 24, 2022
same problem!
quadristan commentedon May 23, 2022
Renke Grunwald (@renke) i have a repro where everything has the same version here : https://github.com/quadristan/ts-indirect-type-reference-bug
agmitron commentedon Jul 10, 2022
same problem :( have you solved it?
renke commentedon 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 commentedon Aug 8, 2022
same problem
kiastorm commentedon Aug 24, 2022
I got this issue because the mentioned dependency had this code in its
index.d.ts:and the file it was complaining in was using
React.HTMLAttributes<HTMLElement>which referencesReact.DOMAttributes. I worked around this issue by omitting the declared propertiesOmit<React.HTMLAttribute<HTMLElement>, "css">Peroconino commentedon Aug 26, 2022
Same problem :(
vaibhavkumar-sf commentedon Sep 2, 2022
Same Problem for me too :(
ivanbanov commentedon Sep 12, 2022
is there any expected timeline for a fix for this problem?
grmkris commentedon Sep 15, 2022
same here
patroza commentedon Sep 16, 2022
Ryan Cavanaugh (@RyanCavanaugh)
100 remaining items
Dorianslavov commentedon Feb 23, 2024
I have managed to make the error go away by making a TypeScript Reference Type at the top of the file
In my case I was was importing
useI18nfunction and spreading an object in it's parameter object likeuseI18n({ ...someObj })which caused this
The inferred type of useI18n cannot be named without a reference to .pnpm/@[email protected]/node_modules/@intlify/core-base . This is likely not portable. A type annotation is necessary.✅ Fixed it by doing this
At the top of the file which used
useI18nI've added the TS reference type like so/// <reference types="@intlify/core-base" />So to fix it, basically check the problematic package name ( in my case it was
@intlify/core-base) and add it as a TypeScript Reference Type like in the example aboveLOWE-7566 commentedon Mar 29, 2024
use type casting it is always solved in that way
matheo commentedon Mar 31, 2024
This didn't work for my
Nxmonorepo withpnpmbecause in the workspace, peer dependencies were installed inside the library folder duplicating the package and causing the error. I opted to disable the peerdeps installation with:and reviewed the peer dependencies warnings to confirm that they were installed, and added the ones missing: then I confirmed that everything ran as expected.
fix: 🐛 update `tsconfig.json`
add reference types from dwn-sdk-js to avoid pnpm build error https:/…
add reference types from dwn-sdk-js to avoid pnpm build error https:/…
add reference types from dwn-sdk-js to avoid pnpm build error (#507)
patricktree commentedon May 4, 2024
FYI there is some interesting discussion on this topic in microsoft/TypeScript#58176.
I find this idea for a solution by Sebastian "Sebbie" Silbermann (@eps1lon) interesting (which is kind of bringing workaround #3.1 of my comment above into tsc itself):
cumt-robin commentedon May 4, 2024
RyanCavanaugh commentedon May 6, 2024
Closing per #42873 (comment)