This seems te happen because Yarn resolves multiple versions of a package; @types/react
in this particular case. Yarn resolves @types/react
from your package.json and as a dependency of @types/react-dom
.
Take the following snippet from my package.json:
"devDependencies": {
"@types/react": "^15.0.16",
"@types/react-dom": "^0.14.23"
...
}
The yarn.lock that is created after you run yarn install
contains something similar to this:
"@types/react-dom@^0.14.23":
version "0.14.23"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-0.14.23.tgz#cecfcfad754b4c2765fe5d29b81b301889ad6c2e"
dependencies:
"@types/react" "*"
"@types/react@*":
version "16.4.14"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.4.14.tgz#47c604c8e46ed674bbdf4aabf82b34b9041c6a04"
dependencies:
"@types/prop-types" "*"
csstype "^2.2.0"
"@types/react@^15.0.16":
version "15.6.19"
resolved "https://registry.yarnpkg.com/@types/react/-/react-15.6.19.tgz#a5de18afe65b0f29767328836b48c498a5d3a91b"
Notice that @types/react-dom
depends on any version of @types/react
as indicated by "*"
. Yarn resolves two versions of @types/react
: "16.4.14"
and "15.6.19"
. This results in the type conflicts you mentioned.
The solution is to add a resolutions field to your package.json to tell Yarn to resolve a specific version of @types/react
. Take the following sample:
"resolutions": {
"@types/react": "^15.0.16"
}
Run yarn install
again. Notice the change in the yarn.lock file:
"@types/react-dom@^0.14.23":
version "0.14.23"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-0.14.23.tgz#cecfcfad754b4c2765fe5d29b81b301889ad6c2e"
dependencies:
"@types/react" "*"
"@types/react@*", "@types/react@^15.0.16":
version "15.6.19"
resolved "https://registry.yarnpkg.com/@types/react/-/react-15.6.19.tgz#a5de18afe65b0f29767328836b48c498a5d3a91b"
Yarn now resolves the same version "15.6.19"
for both "@types/react@*"
and "@types/react@^15.0.16"
dependencies.
I would like to know myself why this is needed. I would expect Yarn to understand it can resolve dependency "@types/react" "*"
with "@types/react@^15.0.16"
instead of resolving it with the latest version of @types/react
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…