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 22, 2022
1 parent 92c5bb5 commit 02ba1bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions types/react/experimental.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,7 @@ declare module '.' {
export type Usable<T> = Thenable<T> | Context<T>;

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

// tslint:disable-next-line ban-types
export function experimental_useEvent<T extends Function>(event: T): T;
}
27 changes: 27 additions & 0 deletions types/react/test/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,33 @@ const everyHookRef = React.createRef<{ id: number }>();
}}/>;

function useExperimentalHooks() {
// Implicit any
// @ts-expect-error
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 02ba1bf

Please sign in to comment.