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

[Bug] [Yarn Berry] TS2742: The inferred type of 'testContext' cannot be named without a reference to './.yarn/cache/...' #36675

Closed
skoging opened this issue Feb 7, 2020 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@skoging
Copy link

skoging commented Feb 7, 2020

TypeScript Version: 3.8.0-dev.20200207
Also tested on 3.5, 3.6 and 3.7 with the same results.

Search Terms: Yarn Berry TS2742 inferred type cannot be named

Code

// a.ts
import { createContext } from "react";

export const testContext = createContext(0);

Example file: https://gist.github.com/skoging/72262b3ac553899b61ad7b6844b3b2b3

yarn init -y
yarn policies set-version (1|2)
yarn add react @types/react typescript
yarn tsc --lib es6 -d a.ts

Expected behavior:

Yarn 1:

Done in 2.06s.

Actual behavior:
Yarn 2

a.ts:3:14 - error TS2742: The inferred type of 'testContext' cannot be named without a reference to './.yarn/cache/@types-react-npm-16.9.19-21809ae185-1.zip/node_modules/@types/react'. This is likely not portable. A type annotation is necessary.

3 export const testContext = createContext(0);
              ~~~~~~~~~~~


Found 1 error.

Playground Link:

Related Issues:
#29808
#32970
#29221
#30858

These other issues are likely due the same underlying problem. But based on the comments on some of them, it seems a separate issue with simple reproduction steps is preferred.

This issue is specifically about this problem in Yarn 2 (Yarn Berry).

Yarn:
yarnpkg/berry#919
yarnpkg/berry#736

@RyanCavanaugh
Copy link
Member

This specific instance is the error message telling you the truth

@skoging
Copy link
Author

skoging commented Mar 4, 2020

import { createContext } from "react";

import type { Context } from "react";

export const testContext = createContext(0);

Just importing the inferred type seems to be enough make this work.

Why is TypeScript unable to infer that ./.yarn/cache/@types-react-npm-16.9.19-21809ae185-1.zip/node_modules/@types/react import path should really just be react (or @types/react)?
Is this due to assumptions that anly work in a normal node_modeles environment?

There seem to be many issues regarding this problem. Having a different import path than the expected ./node_modules/... — be it due to Yarn 2, pnpm, symlinks — always seem to be causing problems.

Is there anything the Yarn team could change on their end to make this work? Some transparency as to how typescript infers the import would certainly help.

@akphi
Copy link

akphi commented Mar 30, 2021

@skoging if you use nodeLinker: node-modules in your .yarnrc.yml (i.e. opt out of Yarn@2 PnP) is it working well?
Also I cannot seem to reproduce the problem on the latest Typescript and Yarn

@skoging
Copy link
Author

skoging commented Mar 30, 2021

This was definitely a PnP issue. We ended up upgraded to Yarn 2 by using nodeLinker: node_modules, in part because this issue and others were blocking us from using PnP. We've since transitioned to PnP, and in recent versions of TS and/or Yarn this issue seems to be fixed.

I'll close the issue.

@skoging skoging closed this as completed Mar 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants