The inferred type of "X" cannot be named without a reference to "Y". This is likely not portable. A type annotation is necessary.

See original GitHub issue

Bug Report

๐Ÿ”Ž Search Terms

inferred type cannot be named, symlink node_modules

๐Ÿ•— Version & Regression Information

Iโ€™m verifying the problem on the typescript@4.1.3. Iโ€™ve not tried older versions but at least is also reproducible on the @next version as of today.

It is probably a regression or a corner case related with other issues opened and already closed like:

โฏ Playground Link

Link for a repo where the problem is being reproduced

NOTE: Just clone the repo and run yarn tsc

๐Ÿ’ป Code

All the relevant code can be found at https://github.com/mistic/reproduce-typescript-problem-when-symlinking-node_modules

It is just reproducing a similar setup that I had on other project that was generating the problem:

  • node_modules are a symlink to another location that is not a direct parent of the symlinked node_modules
  • we are using types in the compilation from a library where those types are just exported from other one, like for example withRouter within react-router-dom that is just a plain export from the same type on react-router.

๐Ÿ™ Actual behavior

I got the following error:

error TS2742: The inferred type of 'Nav' cannot be named without a reference to '../../deps/node_modules/@types/react-router'. This is likely not portable. A type annotation is necessary.

8 export const Nav = withRouter(({ history }: NavProps) => {
               ~~~


Found 1 error.

๐Ÿ™‚ Expected behavior

I was expecting no error at all and that the typescript compiler was just able to find all the respective modules. Iโ€™ve tried everything that I thought was related like enabled the preserveSymlinks. The only thing that stops the error is importing the withRouter type directly from react-router and not from react-router-dom but that doesnโ€™t make very sense because I actually want to use the react-router-dom on a couple of places.

\cc @weswigham @sheetalkamat @andrewbranch because I saw you previously worked to try to solve similar issues.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:77
  • Comments:65 (3 by maintainers)

github_iconTop GitHub Comments

66reactions
paynecodescommented, Jul 23, 2021

I get this issue throughout many scenarios when using a Rush monorepo. Rush, by default, uses pnpm (pnpm workspaces is recommended). I havenโ€™t been able to track down the root cause, but I have scoured this issue tracker and subscribed to many topics hoping someone squashes this.

59reactions
imcuttlecommented, Jan 7, 2022

In my case, add preserveSymlinks for resolve it

{
  "compilerOptions": {
    "preserveSymlinks": true
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

10 - Stack Overflow
When it says. This is likely not portable. A type annotation is necessary. Sometimes you need to check your tsconfig.json and see if...
Read more >
rushstack/rushstack - Gitter
... cannot be named without a reference to '../../../projectA/node_modules/@types/signals'. This is likely not portable. A type annotation is necessary.
Read more >
TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >
error TS2742: The inferred type of X cannot be named without ...
error TS2742: The inferred type of 'closeChannel' cannot be named without a reference to '.pnpm/@polkadot+types@8.14.1/node_modules/@polkadot/ย ...
Read more >
Type Annotation in TypeScript - TutorialsTeacher
Type annotations are used to enforce type checking. It is not mandatory in TypeScript to use type annotations. However, type annotations help the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found