Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion types/react/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ declare namespace React {
interface RefObject<T> {
readonly current: T | null;
}
type RefCallback<T> = { bivarianceHack(instance: T | null): void }["bivarianceHack"];
type RefCallback<T> = (instance: T | null) => void;
type Ref<T> = RefCallback<T> | RefObject<T> | null;
type LegacyRef<T> = string | Ref<T>;
/**
Expand Down
10 changes: 9 additions & 1 deletion types/react/test/tsx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ const divRef = React.createRef<HTMLDivElement>();
<ForwardRef2 ref={divRef}/>;
<ForwardRef2 ref='string'/>; // $ExpectError

const htmlElementFnRef = (instance: HTMLElement | null) => {};
const htmlElementRef = React.createRef<HTMLElement>();
<div ref={htmlElementFnRef} />;
<div ref={htmlElementRef} />;
const unsoundDivFnRef = (instance: HTMLDivElement) => {};
// `instance` is nullable
<div ref={unsoundDivFnRef} />; // $ExpectError

const newContextRef = React.createRef<NewContext>();
<NewContext ref={newContextRef}/>;
<NewContext ref='string'/>;
Expand Down Expand Up @@ -407,7 +415,7 @@ imgProps.loading = 'nonsense';
// $ExpectError
imgProps.decoding = 'nonsense';
type ImgPropsWithRef = React.ComponentPropsWithRef<'img'>;
// $ExpectType ((instance: HTMLImageElement | null) => void) | RefObject<HTMLImageElement> | null | undefined
// $ExpectType RefCallback<HTMLImageElement> | RefObject<HTMLImageElement> | null | undefined
type ImgPropsWithRefRef = ImgPropsWithRef['ref'];
type ImgPropsWithoutRef = React.ComponentPropsWithoutRef<'img'>;
// $ExpectType false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ select('core/block-editor').getAdjacentBlockClientId('foo', 1);

{
const blockProps = be.useBlockProps({ ref: useRef("test") });
// $ExpectType (instance: unknown) => void
// $ExpectType RefCallback<unknown>
blockProps.ref;
}

Expand Down