TypeScript: importHelpers with symlinks breaks type resolution
TypeScript Version: 3.2.2
Search Terms: type symlink importHelpers pnpm
Code https://github.com/stevefan1999-personal/gqlify/tree/patch-pnpm-tslib
Reproduction:
This is a little bit complicated, you will need to clone the repo I provided and checkout the branch, then run pnpm install, and then please flip the importHelpers options to false and re-run the installation again.
Expected behavior:
When importHelpers is true, it shouldn’t break type inference like we should when importHelpers is false.
Actual behavior:
error TS2742: The inferred type of '...' cannot be named without a reference to '...'. This is likely not portable. A type annotation is necessary.
When importHelpers is true and the @types directory is a symlink.
Related Issues: https://github.com/pnpm/pnpm/issues/1375
Extra Stuff: Although it is application-specific (Lerna with NPM/Yarn works fine on this), I do think this should be handled correctly regardless of any layers of symlinks.
PS: tsc output observation diff: http://www.mergely.com/a6N4kOW3/
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 29
- Comments: 31 (5 by maintainers)
Commits related to this issue
- Fix CI instability when building Typescript The error appears to happen when symlinking typing files and manifests with the error: ``` lib/actions-oclif/env/add.ts(73,16): error TS2742: The inferred ... — committed to balena-io/balena-cli by LucianBuzzo 5 years ago
- Fix CI instability when building Typescript The error appears to happen when symlinking typing files and manifests on case sensitive file systems (like windows) with the error: ``` lib/actions-oclif/... — committed to balena-io/balena-cli by LucianBuzzo 5 years ago
- add explicit flag type annotations per https://github.com/microsoft/TypeScript/issues/29221 to fix downstream build Change-Id: I9eee44451b2ed41382cda9a446c47fc5c8f39e33 Signed-off-by: nickboldt <nbol... — committed to nickboldt/chectl by nickboldt 4 years ago
- add explicit flag type annotations per https://github.com/microsoft/TypeScript/issues/29221 to fix downstream build Change-Id: I9eee44451b2ed41382cda9a446c47fc5c8f39e33 Signed-off-by: nickboldt <nbol... — committed to nickboldt/chectl by nickboldt 4 years ago
- fix: add explicit flag type annotations per https://github.com/microsoft/TypeScript/issues/29221 to fix downstream build Change-Id: I9eee44451b2ed41382cda9a446c47fc5c8f39e33 Signed-off-by: nickboldt ... — committed to nickboldt/chectl by nickboldt 4 years ago
- fix: add explicit flag type annotations per https://github.com/microsoft/TypeScript/issues/29221 to fix downstream build Change-Id: I9eee44451b2ed41382cda9a446c47fc5c8f39e33 Signed-off-by: nickboldt ... — committed to nickboldt/chectl by nickboldt 4 years ago
- fix: add explicit flag type annotations per https://github.com/microsoft/TypeScript/issues/29221 to fix downstream build (#920) Change-Id: I9eee44451b2ed41382cda9a446c47fc5c8f39e33 Signed-off-by: ni... — committed to che-incubator/chectl by nickboldt 4 years ago
- fix: add explicit flag type annotations per https://github.com/microsoft/TypeScript/issues/29221 to fix downstream build (#920) Change-Id: Ic1ae2b8eac3b716e24e7c4ca2b255180f84b24f3 Signed-off-by: nic... — committed to redhat-developer/devspaces-chectl by nickboldt 4 years ago
- fix: add explicit flag type annotations per https://github.com/microsoft/TypeScript/issues/29221 to fix downstream build (#921) Change-Id: I9eee44451b2ed41382cda9a446c47fc5c8f39e33 Signed-off-by: ni... — committed to che-incubator/chectl by nickboldt 4 years ago
- fix: add explicit flag type annotations per https://github.com/microsoft/TypeScript/issues/29221 to fix downstream build Change-Id: I53108482296bec95f0109cfd15aa580973945f4b Signed-off-by: nickboldt ... — committed to che-incubator/chectl by nickboldt 4 years ago
- fix: add explicit flag type annotations per https://github.com/microsoft/TypeScript/issues/29221 to fix downstream build Change-Id: I53108482296bec95f0109cfd15aa580973945f4b Signed-off-by: nickboldt ... — committed to nickboldt/chectl by nickboldt 4 years ago
- fix: add explicit flag type annotations per https://github.com/microsoft/TypeScript/issues/29221 to fix downstream build (#935) Change-Id: I53108482296bec95f0109cfd15aa580973945f4b Signed-off-by: ni... — committed to che-incubator/chectl by nickboldt 4 years ago
- fix: typescript type inference issue. explicit type declaration is required See this for more - https://github.com/microsoft/TypeScript/issues/29221 — committed to chitchatjs/chitchatjs by kevindra 4 years ago
This issue is really annoying! Why is closed?
We are encountering a similar issue, using Rush with pnpm (which symlinks packages in
node_modules). I believe this issue should be reopened.@weswigham can this be reopened? The repro I’ve provided via this comment is still broken in
3.5.2Well, I’ve not only encountered this thing, but got a minimal repro with the weird behavior hapening on 3 computers: https://github.com/DovydasNavickas/ts-2742
I’m just not sure whether this is a TypeScript or VS Code bug. Same thing happens in the latest stable and latest insider versions of VS Code with all
3.3,3.3.3333and nightly3.4.0-dev.20190315versions.When you clone the repo, just run
pnpm installand go totest.tsfile. The error should be there:If it’s not there, restart VS Code and you should see it at least then.
NB! If run the compiler with
pnpx tsc, you will _NOT_ get an errorNow, go to
tsconfig.jsonand comment either"declaration": trueor"importHelpers": trueline and restart VS Code. The error will go away when you comment either config option.The error also goes away when you import
Reactitself along withcreateContextfunction:import React, { createContext } from "react";But then I get another error:
So yeah. Compiler does not complain about this, but VS Code does.
I’m getting the same error when using oclif to scaffold some code. Some of the generated code looks like this:
I get the following error:
The inferred type of 'flags' cannot be named without a reference to '.registry.npmjs.org/@oclif/command/1.5.13/node_modules/@oclif/parser/lib/flags'. This is likely not portable. A type annotation is necessary.However, adapting @jackTheRipper’s solution, I added an explicit type annotation like so:
Adding the annotation made the error go away.
The reason this issue is closed is because there’s nothing actionable in the thread at this point, and what was actionable was fixed, best as could be observed - if you have a codebase that still has an issue on the latest builds, opening a new issue with a full set of reproduction steps (as these kinds of issues are super sensitive to compiler settings, host environment, and actual code layout on disk) would be useful and tracking down and squashing the bug.
But in many instances, seeing the error reported isn’t a bug - even in
@simonfox’s example, it probably isn’t. Why, you ask? Because if TypeScript needs to load two differing versions of one of your dependencies (because you upgraded one dep that shared a different dep with you but did not update that secondary dep within yourself) and one of them is nested, while we can often typecheck in the face of that, thanks to structual checking, we definitely have no stable, safe way to say “the type at this position refers to the older version of the type that we found in the nested node module here”. We could only do one of two things - incorrectly map to the later version, hoping it’s close enough, or issue an error. We err on the side of caution. Having consistent versions of your dependencies is a good thing~ (and, failing that, I think you can override how we resolve the dependency in question with a path mapping to force us to, for example, always resolve to the root version even if node module resolution would normally choose a nested one)We’re having this problem with Bazel-built typescript as well. Bazel symlinks all the
node_modulesdirectories.Run into this with any pnpm monorepo using
@types/would be interested in any solution.Got the same issue in my project. I resolved the error by being more explicit regarding the return type.
So instead of writing the following:
I changed it to
Now it compiles with no tricks
I’ve got the same issue when running in a @microsoft/rush monorepo
The inferred type of 'getUrl' cannot be named without a reference to '../../../../../../../common/temp/node_modules/reselect/lib'. This is likely not portable. A type annotation is necessary.ts(2742).I’m not a TS genius at all, so any help would be appreciated.Similar problem here and I guess the reason is the same. Typescript 3.2.2. Downgrade to 3.1.3 and everthing is fine.
Works fine on Mac and Ubuntu but breaks on Windows, which is using NTFS for sure.
Ooook. More info: I’ve been testing with
--noEmitflag present, which is why I was not getting the error while running the compiler.Turns out, when I removed the flag for outputs to be generated, I got the error here too.
I guess the declarations part is the one erroring out, isn’t it?
/cc @SeaRyanC