Declare @types/react as an optional peer dependency

未关闭 适合新手
#10,940 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript

调研方向

首先检查 react-aria-components、react-aria、react-stately 和 @react-types/shared 的包清单中的 peerDependencies 和 peerDependenciesMeta,然后比较它们现有的 React peer 范围。为受影响的包添加可选的 @types/react peer 元数据,并验证清单和生成的包元数据都包含该信息。完成标准是:这些包声明了可选 peer,且提供的 TypeScript 解析复现能够正确解析 React 类型。

由索引模型根据 Issue 内容生成。

描述

Summary

react-aria-components, react-aria, react-stately, @react-types/shared ship type declarations that import from react but don't declare @types/react.

With an install layout where packages live outside the project, such as pnpm's global virtual store (virtualStoreType: global), TypeScript resolves react from the package's real path. The react peer is linked next to the package, @types/react is not, so TypeScript picks the untyped react/index.js and every React type in these declarations silently becomes any.

Consumers see it as errors in their own code, for example TS7006: Parameter 'x' implicitly has an 'any' type on callback parameters, or component props that lose children and className. Under pnpm's default layout it only works through pnpm's hidden hoisting fallback (node_modules/.pnpm/node_modules).

Affected versions: [email protected], [email protected], [email protected], @react-types/[email protected]

Reproduction

pnpm 12.18.0:

mkdir repro && cd repro
echo '{ "name": "repro", "private": true }' > package.json
echo 'virtualStoreType: global' > pnpm-workspace.yaml
pnpm add react-aria-components react react-dom @types/react typescript
echo 'import "react-aria-components";' > index.ts
pnpm exec tsc --noEmit --traceResolution --module esnext --moduleResolution bundler --jsx react-jsx index.ts | grep "Module name 'react' was"

Expected: react resolves to @types/react/index.d.ts.

Actual: .../pnpm/store/v11/links/@/react/<version>/<hash>/node_modules/react/index.js

Suggested fix

"peerDependencies": {
    "@types/react": "*"
},
"peerDependenciesMeta": {
    "@types/react": { "optional": true }
}

Optional, so JavaScript consumers are unaffected. The range can mirror the existing react peer range. react-router, @tanstack/react-query and recharts (recharts#7710) do the same.

Consumer-side workarounds until then: pnpm's addMissingPeerTypes (pnpm 12.12+) or packageExtensions. Related: pnpm/pnpm#9739.

主要语言
TypeScript
星标
16k
派生
1.6k
平均合并
4 天 3 小时
30 天内合并 PR
39

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

adobe/react-spectrum 的其他 Issue

查看 adobe/react-spectrum 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。