TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

"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

Open renke opened this issue 3 years ago • 96 comments

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 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/@[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.

renke avatar Jan 30 '22 09:01 renke

same problem

zengguirong avatar Feb 09 '22 02:02 zengguirong

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.

renke avatar Mar 16 '22 09:03 renke

same problem!

dohooo avatar Apr 24 '22 10:04 dohooo

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

quadristan avatar May 23 '22 00:05 quadristan

same problem :( have you solved it?

agmitron avatar Jul 10 '22 16:07 agmitron

I haven't solved it yet, but there is also a similar issue https://github.com/microsoft/TypeScript/issues/48212 with a milestone of TypeScript 4.8.0. Let's hope it will be solved soon.

renke avatar Jul 15 '22 09:07 renke

same problem

StarHosea avatar Aug 08 '22 17:08 StarHosea

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">

kiastorm avatar Aug 24 '22 19:08 kiastorm

Same problem :(

Peroconino avatar Aug 26 '22 12:08 Peroconino

Same Problem for me too :(

vaibhavkumar-sf avatar Sep 02 '22 16:09 vaibhavkumar-sf

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

ivanbanov avatar Sep 12 '22 10:09 ivanbanov

same here

grmkris avatar Sep 15 '22 22:09 grmkris

@RyanCavanaugh

patroza avatar Sep 16 '22 04:09 patroza

Same problem :(

yingpengsha avatar Sep 21 '22 03:09 yingpengsha

same here

Evertt avatar Sep 26 '22 05:09 Evertt

same here

xlboy avatar Sep 28 '22 08:09 xlboy

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.

shellscape avatar Sep 30 '22 15:09 shellscape

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

mrmeku avatar Oct 04 '22 22:10 mrmeku

I've been deep-diving the issues related to this error and symlinks and pnpm specifically. Since 2019 it appears that this regression is reintroduced at least twice for each major version. That would suggest that tests are not sufficient for the case, nor to catch the regression.

shellscape avatar Oct 06 '22 13:10 shellscape

I have a repro + a workaround/solution here

https://github.com/quadristan/ts-indirect-type-reference-bug

tl-dr:


import type {} from "Y"; // or whatever Y really refers to

quadristan avatar Oct 06 '22 21:10 quadristan

I have a repro + a workaround/solution here

https://github.com/quadristan/ts-indirect-type-reference-bug

tl-dr:

import type {} from "Y"; // or whatever Y really refers to

Nice job! This is by far the best practice.👍

i7eo avatar Oct 16 '22 03:10 i7eo

I've been deep-diving the issues related to this error and symlinks and pnpm specifically. Since 2019 it appears that this regression is reintroduced at least twice for each major version. That would suggest that tests are not sufficient for the case, nor to catch the regression.

The reason you see a history of this "regressing" at each version is that pnpm patches TypeScript when you install it, so every time TS updates, there's a few days where it appears to not work because pnpm hasn't updated their patching code yet.

RyanCavanaugh avatar Oct 17 '22 20:10 RyanCavanaugh

@RyanCavanaugh really? Do PNPM is patching TS in order for it to be compatible each and every time it installs it? That's a huge surprise for me really. Feels very unsustainable and kinda scary. Why is TSC itself is not compatible with PNPM? I hear about the first time in my life and never saw anything like that in the PNPM change log (not that I reading them (aside from major releases)) :)

RIP21 avatar Oct 17 '22 20:10 RIP21

I've been deep-diving the issues related to this error and symlinks and pnpm specifically. Since 2019 it appears that this regression is reintroduced at least twice for each major version. That would suggest that tests are not sufficient for the case, nor to catch the regression.

The reason you see a history of this "regressing" at each version is that pnpm patches TypeScript when you install it, so every time TS updates, there's a few days where it appears to not work because pnpm hasn't updated their patching code yet.

could you point me at the code responsible for that? I'm a core contributor to PNPM and this is the first time I'm hearing of it. not to say that I disagree with you or that it isn't true, I'm just unaware of that.

shellscape avatar Oct 17 '22 20:10 shellscape

Sorry, you're right. I've misconfused this with yarn.

RyanCavanaugh avatar Oct 17 '22 21:10 RyanCavanaugh

@shellscape while I've got you here, we've been trying to reach security@ pnpm.io regarding an issue we identified a while back, but haven't received a reply. Can you poke the appropriate folks to take a look?

RyanCavanaugh avatar Oct 20 '22 17:10 RyanCavanaugh

sure thing. feel free to DM me info on twitter as well.

shellscape avatar Oct 20 '22 18:10 shellscape

In my case the error looks like this

The inferred type of 'Foo' cannot be named without a reference to '.pnpm/@[email protected]_.../node_modules/@morphic-ts/model-algebras/lib/types'. This is likely not portable. A type annotation is necessary

The type definition wasn't exported from root of the package but it was from/lib/types.

So instead of just importing the package:

import type {} from '@morphic-ts/model-algebras'

I had to specify the path which the type definition was exported:

import type {} from '@morphic-ts/model-algebras/lib/types'

aabccd021 avatar Oct 22 '22 23:10 aabccd021

@quadristan

You saved my life! I added:

import type {} from 'react'

This is definitely a super-nasty TypeScript bug. One of the worst I've encountered because it can't be ignored and, in this case, took me hours to debug and find this solution.

matthew-dean avatar Nov 30 '22 19:11 matthew-dean

import type {} from "Y"

Oooh! Thank you so much!! And this can be done in the root index.ts or anywhere really. So good. 😄

CarlRibbegaardh avatar Dec 01 '22 20:12 CarlRibbegaardh