-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[utils] Improve type inference of useForkRef #31845
Conversation
@@ -72,7 +72,7 @@ function useSelect<TValue>(props: UseSelectParameters<TValue>) { | |||
} | |||
}, [listboxFocusRequested]); | |||
|
|||
const updateListboxRef = (listboxElement: HTMLUListElement) => { | |||
const updateListboxRef = (listboxElement: HTMLUListElement | null) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was unsound
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would basically need to always add | null
when using ref callbacks no?
@@ -154,7 +154,7 @@ const Link = React.forwardRef(function Link(inProps, ref) { | |||
as={component} | |||
onBlur={handleBlur} | |||
onFocus={handleFocus} | |||
ref={handlerRef} | |||
ref={handleRef} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a typo-drive-by-fix
@@ -115,7 +115,7 @@ const Link = React.forwardRef(function Link(inProps, ref) { | |||
ref: focusVisibleRef, | |||
} = useIsFocusVisible(); | |||
const [focusVisible, setFocusVisible] = React.useState<boolean>(false); | |||
const handlerRef = useForkRef(ref, focusVisibleRef) as React.RefObject<HTMLSpanElement>; | |||
const handleRef = useForkRef(ref, focusVisibleRef) as React.Ref<HTMLSpanElement>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cast was unsound generally but also technically incorrect at runtime. useForkRef
returns a function not an object (though the specific implementation is irrelevant. What's import is, that it's a generic ref).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch
f8eac87
to
4b5d2bb
Compare
export default function useForkRef<InstanceA, InstanceB>( | ||
refA: React.Ref<InstanceA> | null | undefined, | ||
refB: React.Ref<InstanceB> | null | undefined, | ||
): React.Ref<InstanceA & InstanceB> | null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This helps TypeScript with inference with the proposed changes in DefinitelyTyped/DefinitelyTyped#58936.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this will basically create a union of the two in case they are different 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good, looks like we will need to do more for the actual upgrade to the latest @types/react, but I will be pushing the changes in separate PRs.
Currently instances in ref callbacks are allowed to be nullable due to our bivariance hack. However, during runtime these instances can be null: https://codesandbox.io/s/refs-are-nullable-m44vxx
We'll likely remove the bivariance hack to catch these issues in the future. In the meantime, existing packages and tests should guard against nullable instances regardless.
The issue was first reported in DefinitelyTyped/DefinitelyTyped#58464