"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
See original GitHub issueBug 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/@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
βββ @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/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.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:43
- Comments:41 (3 by maintainers)
Top Related StackOverflow Question
I have a repro + a workaround/solution here
https://github.com/quadristan/ts-indirect-type-reference-bug
tl-dr:
same problem