fix: Fixes an issue where the target does not accept the ref type when using explicit generics with useEventListener. - #2903
Merged
crazylxr merged 1 commit intoMar 28, 2026
Conversation
commit: |
Collaborator
|
你这个所有的 title 都改成 英文吧, 等下我找 萧看下 |
Contributor
Author
|
ok,runAsync那个我还要再改一下,后续再一起标题改成英文下
…---原始邮件---
发件人: ***@***.***>
发送时间: 2026年2月24日(周二) 下午3:39
收件人: ***@***.***>;
抄送: "sheng ***@***.******@***.***>;
主题: Re: [alibaba/hooks] fix: 修复 useEventListener 显式泛型时 target 不接受 ref 的类型问题 (PR #2903)
meet-student left a comment (alibaba/hooks#2903)
你这个所有的 title 都改成 英文吧, 等下我找 萧看下
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
meet-student
approved these changes
Feb 24, 2026
Collaborator
|
LGTM,这个修复方向没问题,直接命中了显式泛型场景下 target 不能接收 ref 的根因。补的回归用例也比较到位,而且这条用例会参与 TypeScript 检查,能防住后续类似回归。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤔 这个变动的性质是?
🔗 相关 Issue
💡 需求背景和解决方案
Issue #2902 中,
useEventListener在显式指定事件泛型参数时(例如useEventListener<'scroll'>),options.target的类型推导会退化为HTMLElement本体,导致ref(如RefObject<HTMLDivElement | null>)无法通过类型检查。具体修复:
useEventListener内部Options<T>类型定义:target?: Ttarget?: BasicTarget<T>涉及文件:
packages/hooks/src/useEventListener/index.tspackages/hooks/src/useEventListener/__tests__/index.spec.ts📝 更新日志
useEventListenertyping to acceptreftargets when event generic is explicitly specified (e.g.useEventListener<'scroll'>). Added a regression test for this scenario.useEventListener在显式指定事件泛型(如useEventListener<'scroll'>)时target不能接收ref的类型问题,并补充对应回归测试。☑️ 请求合并前的自查清单