TypeScript: Package dependencies and The inferred type of ... cannot be named without a reference to ...
TypeScript Version: 3.3.0-dev.20190119
Search Terms: inferred type, cannot be named, reference, package, dependency
This isn’t really a traditional bug report. Mostly a PSA of some sort.
Code
The dependencies of interest are,
- My project has a direct dependency to “A”
- My project has a direct dependency to “B”
- “A” has a direct dependency to “B”
Before updating,
- My project has
1.0.0of package “A” (older version) - My project has
2.51.1of package “B” (older version) 1.0.0of package “A” has2.51.1of package “B” (older version)
After updating package “A”,
- My project has
1.0.1of package “A” (newer patch version, bug-fix, no.d.tschanges) - My project has
2.51.1of package “B” (older version) 1.0.1of package “A” has2.51.2of package “B” (newer patch version, bug-fix, no.d.tschanges)
There’s a package version mismatch here.
My project has 2.51.1 of “B” but the package I updated has 2.51.2 of “B”.
In the newer version of “A” and “B”, no .d.ts files were added/removed/modified.
Expected
Before updating, the project compiled. So, the rationale is that after updating a patch version, the project should still compile.
Actual
After updating, the project stopped compiling.
tsc crashed; out-of-memory exception.
VS Code gave me the error,
The inferred type of … cannot be named without a reference to
node_modules/A/node_modules/B
Workaround
The workaround is to just update “B” to 2.51.2 in my project. After doing so, everything compiles again.
It seems like package versions matter to TypeScript when it comes to transitive dependencies. Even if it’s a patch version and no .d.ts files are added/removed/modified.
Related Issues: #29221
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 29
- Comments: 34 (4 by maintainers)
Commits related to this issue
- Fix ts2742 when resolving ts-common types This error has been plaguing me for ages. It appears to be caused by the static inferred type of an io-ts codec depending on a type in a transitive dependenc... — committed to bitaccess/coinlib by dylanseago 5 years ago
- Fix VCode error: https://github.com/microsoft/TypeScript/issues/29808#issuecomment-487811832 — committed to liujingbreak/plink by liujingbreak 4 years ago
solved with Module resolution:
Happens when we have conflicting versions of a package installed. A temporary solution would be to remove the package mentioned from peer dependencies of the plugin giving the error.
Or install the exact version of common dependancy
Try with:
Credits to @imcuttle https://github.com/microsoft/TypeScript/issues/42873#issuecomment-1007540868
There are workarounds for this, though none of which I have liked. In my case, importing the package whose type was used and is considered “private” gets around this.
For example, if you had defined a type in package
XcalledExampleAthat referenced typeExampleBin packageY, and it complained that it references private typeExampleBfromnode_modules/Y, you can doThis will, however, not work if you use that package only as a dev dependency.
A workaround would be to use
import typebut that does not allow side-effect imports (e.g.import type "Y"is not valid)You could import the type that causes the issue, such as
Again though, none of these are that pretty. I hope that a better solution can be proposed here because it makes more advanced types absolutely worthless or introduces ugly code with necessary comments pointing to this issue 😞
Same issue here. With Typescript 4.1.5.
I wonder why ist this still open, since it’s created on February 2019 😄
Hello, I had the same issue!
As a POC, I created a repository dedicated to this issue, to show how it is happening! Repository: https://github.com/nogueira7egend/ts-symlink-error/
Screenshot Error:
Please check it and follow the instructions that are on README! Best regards
Yep, just like everyone else is hinting, this problem happens when a dependency that is linked into the project (f.e. with
npm link, or the yarn/pnpm/etc linking tools) contains the same dependency as the project does.In this case, the dependency is not de-duped, and exists in two places: in the project’s
node_modules, and innode_module/linked-dependency/node_modules.For some reason, VS Code (TypeScript?) is preferring the nested dependency, even if they have the same version number, and (for some reason) introducing this weird error.
One way that I get around the problem is, instead of linking a project (which is a difficult thing not to do if you are relying on workspace tools like yarn/npm), to put that dependency’s ref/hash in the dependent’s package.json, so that
npm installwill install the dependency from VCS (f.e. from a branch that has the work-in-progress changes). In this case, there’s no error, as NPM will successfully de-dupe the duplicate dependencies.I haven’t observed if this issue happens with duplicate dependencies in non-linked projects (f.e. which could happen if the duplicate dependencies have incompatible versions and can not be de-duped).
As a general way to reproduce, make sure your project has this structure in node_modules:
And that might generate the error.
one workaround
We can install the dependency from VCS’s like git (note, if the dependency has build steps, this won’t work unless that dependency stores build output in the code repository, or has a prepare script that tells NPM how to build the project):
in
package.json:where
REFis a branch name, a commit hash, or a tag, etc.Once that is in
package.json, avoid linkingthe-linked-dependencyso it will no longer be symlinked, and runnpm installfor it to be installed from VCS (f.e. from GitHub in this case).I had the same issue and i solved it by changing the name of some declaration file
d.tsto the actual name of the component I am trying to add types too so in my case it was: before:@types/styled.d.tsafter@types/styled-components.d.tswherestyled-componentsis the full npm package name and the error is goneOh and probably importantly considering this seems to be exposed via symlinks, I am on Windows 10
"preserveSymlinks": truedoesn’t resolve all errors that I have. I also usepnpmandtypescripttogether. Previously I usedyarnandtypescriptand everything was fine. Now, I haveThe inferred type of 'configSchema' cannot be named without a reference to '@pnpm-test/core-config/node_modules/yup/lib/string'. This is likely not portable. A type annotation is necessary.and have no idea how to resolve that.I also set
declaration: falsefor testing purposes but had no luck having it working.It does work for link errors using pnpm. thank you very much.
@givebk-bot !donate @antoniopresto $ 5
thanks for your solution! old but gold. ❤️
I also have a problem like this inside a monorepo, and just reinstalling the deps with
yarnsolves it. It’s happened a few times. Don’t know it could be something to do with theyarn.lockfile getting out of sync after merging branches or something. The regenerated lock filed always shows a diff around the package referenced in the TS error.Don’t really understand what’s going on but hope that helps someone.
Got similar error in Lerna mono-repo with yarn workspace
Work-around was to disable
aws-appsyncpackage hoisting in mypackage.jsonSame bug TS 3.7.2. The APP uses some REACT stuff imported indirectly (APP has no direct REACT dependency) via intermediary LIB.
TypeScript complains some REACT interface passed indirectly through LIB cannot be named without a reference to
LIB/node_modules/REACT.Temporary solution: set
declaration: falseintsconfig.jsonin APP.P.S. Dependency structure
I ran into the same issue after linking packages in a yarn workspace
in my case, I was referencing
Reactand it was being picked up as a global type rather than as an import i.e.import * as React from 'react'changing the name so it doesn’t refer to a global alias fixed my issue e.g.
import * as R from 'react'edit: error came back again, so not sure if the above was the actual fix 😓
@RyanCavanaugh we are bumping into this issue in an Aurelia app we are building.
It has appeared after upgrade from TS 3.1.x (now on 3.4.5).
For us the problem is exposed with
declaration:truewhen weyarn linkdifferent packages together for dev.I’ve been trying to put together a repro for you and have got something although it’s not perfect (problem is only visible via VS Code and I havent been able to determine the difference in the CLI build).
If you clone
https://github.com/simonfox/repro-plugin-oneandhttps://github.com/simonfox/repro-plugin-twoand run yarn for both. And then in plugin-one runyarn link plugin-two. Then you will see the issue insrc/features/feature-one/actions.ts(you may need to reload the VS Code window after linking).