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] TS2742 The inferred type of … cannot be named without a reference to … #227

Closed
bradleyayers opened this issue Jun 10, 2019 · 7 comments
Labels
bug Something isn't working

Comments

@bradleyayers
Copy link
Contributor

Describe the bug

When using pnpify, ttypescript, and yarn workspaces, trying to compile (pnpify ttsc -b -p tsconfig.json) one of my workspaces I receive numerous TS2742 errors, one example is:

src/routing/RoutingContext.tsx:21:14 - error TS2742: The inferred type of 'RoutingContext' cannot be named without a reference to '../../../../.yarn/cache/@types-prop-types-npm-15.5.5-d376afe0878cf73ef7826a6fbbde3b8bbd763fa63a05ffcc0b452aaca80cefd1.zip/node_modules/@types/prop-types/node_modules/@types/react'. This is likely not portable. A type annotation is necessary.

In one of my workspaces there's 20 of these, so I could go around and actually add type annotations to these, but it would be a regression in developer experience to have to do this. Looks potentially related to microsoft/TypeScript#29221

To Reproduce

Not sure what the minimal reproduction is, I can do more investigation if this issue is foreign and needs more explanation.

Environment if relevant (please complete the following information):

  • OS: OSX
  • Node version 10.9.0
  • TypeScript 3.5.1
  • Yarn version 2

Additional context

image

@bradleyayers bradleyayers added the bug Something isn't working label Jun 10, 2019
@bradleyayers
Copy link
Contributor Author

Turns out this was due to React not being imported in the files, yet was seemingly made available globally implicitly. Adding import React from "react"; to the top of problematic files fixed this.

@bradleyayers
Copy link
Contributor Author

There was actually another set of these errors that came from having two versions of @types/react installed. I think a second version was pulled in via transitive dependency so adding a "@types/react": "^16.8.19", to my root workspace resolutions fixed it.

@flyskywhy
Copy link

I solved it by move project position to a clean folder which has no parent node_modules/. For example, if

npm install @textile/js-http-client
cd node_modules/@textile/js-http-client/
npm install
./node_modules/.bin/tsc

will get error:

src/modules/account.ts:53:3 - error TS2742: The inferred type of 'sync' cannot be named without a reference to '@textile/js-http-client/node_modules/web-streams-polyfill'. This is likely not portable. A type annotation is necessary.

53   sync(apply?: boolean, wait?: number) {

but if

mv node_modules/@textile/js-http-client/ ../
cd ../js-http-client/
npm install
./node_modules/.bin/tsc

everything is OK.

@anupam-io
Copy link

anupam-io commented Dec 13, 2022

Happened because not defineing the return type of an async function. I defined the return type as Promise<any>, and the error went away.

@marek-hanzal
Copy link

I've encountered the same problem (it's not related to this issue), but may help: I do not want to put dummy imports around the files, so I've tried to add just types to tsconfig.json aaaaaand! It helped!
image

@InfiniteXyy
Copy link

I also encountered this problem in monorepo. My solution is to remove the exclude: ["node_modules"] in the public pacakge.

@marek-hanzal
Copy link

I've another note on the topic: I've removed PNPM and used just NPM in the cost of less obvious phantom package detection (but lint covers this). 99% of TS problems dissapeared when I've switch to NPM (or maybe YARN).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants