ITADN

Phantom dependency / missing peer dependency on `@types/react`

#230Openkcon-stackav 创建于 2025-03-10
K
kcon-stackavcommented
I ran into https://github.com/pnpm/pnpm/issues/7158 while using `react-mosaic-component` in a monorepo managed by `pnpm` that uses multiple different versions of react. In this environment, even though I'm trying to use `react-mosaic-component` with `react@18` and `@types/react@18` in a sub-project's `package.json`, typescript picks up and uses `@types/react@19` from the monorepo's root `package.json` file instead, leading to an error like the following (among others): ``` error TS2339: Property 'mosaicActions' does not exist on type 'unknown'. ``` for code like this: ```typescript import React, { useContext, } from "react"; import { MosaicContext, } from "react-mosaic-component"; ... const { mosaicActions } = useContext(MosaicContext); ``` as observed by hovering over the `'react'` literal in the `import React from 'react'` import line in my project's copy of `node_modules/.pnpm/react-mosaic-component@6.1.0_@types+node@22.13.9_@types+react@18.3.13_dnd-core@16.0.1_r_5e51779fec180380ca3b15fa496dbe8d/node_modules/react-mosaic-component/lib/contextTypes.d.ts` in VS Code and seeing the TypeScript intellisense report: ``` module "<MY_PROJECT>/node_modules/.pnpm/@types+react@19.0.10/node_modules/@types/react/index" ``` From https://github.com/pnpm/pnpm/issues/7158#issuecomment-1888154963 I understand the issue may be that `react-mosaic-component` does not declare a peer dependency on `@types/react` (but rather just a dev dependency): > I've found that this is caused by third-party packages which include type declarations that use react types and have a phantom dependency on `@types/react`. These packages should list `@types/react` in `peerDependencies` but it's fairly common for them to have it only as dev dependency, because with npm's hoisting behavior that phantom dependency typically won't cause issues. And sure enough if I add the following to my root `package.json` file then all of the errors disappear and my builds/tests pass: ```json "packageExtensions": { "react-mosaic-component": { "peerDependencies": { "@types/react": "*" } } } ``` Would you consider adding a peer dependency on `@types/react` to this project's `package.json` file so we could avoid these errors? 🙏
0 条评论