Skip to content

Commit

Permalink
[react] Add types for experimental_useEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
std4453 committed Sep 21, 2022
1 parent 92c5bb5 commit 282da70
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions types/react/experimental.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,6 @@ declare module '.' {
export type Usable<T> = Thenable<T> | Context<T>;

export function experimental_use<T>(usable: Usable<T>): T;

export function experimental_useEvent<T extends (...args: any[]) => any>(callback: T): T;
}
26 changes: 26 additions & 0 deletions types/react/test/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,32 @@ const everyHookRef = React.createRef<{ id: number }>();
}}/>;

function useExperimentalHooks() {
// Implicit any
const anyEvent = React.experimental_useEvent(value => {
// $ExpectType any
return value;
});
// $ExpectType any
anyEvent({});
// $ExpectType (value: string) => number
const typedEvent = React.experimental_useEvent((value: string) => {
return Number(value);
});
// $ExpectType number
typedEvent('1');
// Argument of type '{}' is not assignable to parameter of type 'string'.
// @ts-expect-error
typedEvent({});

function useContextuallyTypedEvent(fn: (event: Event) => string) {}
useContextuallyTypedEvent(
React.experimental_useEvent(event => {
// $ExpectType Event
event;
return String(event);
}),
);

const [toggle, setToggle] = React.useState(false);

const [done, startTransition] = React.useTransition();
Expand Down

0 comments on commit 282da70

Please sign in to comment.