Skip to content

Commit 02ba1bf

Browse files
committed
[react] Add types for experimental_useEvent
- [`experimental_useEvent` entrypoint](https://unpkg.com/browse/[email protected]/cjs/react.development.js) - [implementation](https://unpkg.com/browse/[email protected]/cjs/react-dom.development.js) - [experimental_useEvent](facebook/react#25229)
1 parent 92c5bb5 commit 02ba1bf

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

types/react/experimental.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,7 @@ declare module '.' {
129129
export type Usable<T> = Thenable<T> | Context<T>;
130130

131131
export function experimental_use<T>(usable: Usable<T>): T;
132+
133+
// tslint:disable-next-line ban-types
134+
export function experimental_useEvent<T extends Function>(event: T): T;
132135
}

types/react/test/hooks.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,33 @@ const everyHookRef = React.createRef<{ id: number }>();
263263
}}/>;
264264

265265
function useExperimentalHooks() {
266+
// Implicit any
267+
// @ts-expect-error
268+
const anyEvent = React.experimental_useEvent(value => {
269+
// $ExpectType any
270+
return value;
271+
});
272+
// $ExpectType any
273+
anyEvent({});
274+
// $ExpectType (value: string) => number
275+
const typedEvent = React.experimental_useEvent((value: string) => {
276+
return Number(value);
277+
});
278+
// $ExpectType number
279+
typedEvent('1');
280+
// Argument of type '{}' is not assignable to parameter of type 'string'.
281+
// @ts-expect-error
282+
typedEvent({});
283+
284+
function useContextuallyTypedEvent(fn: (event: Event) => string) {}
285+
useContextuallyTypedEvent(
286+
React.experimental_useEvent(event => {
287+
// $ExpectType Event
288+
event;
289+
return String(event);
290+
}),
291+
);
292+
266293
const [toggle, setToggle] = React.useState(false);
267294

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

0 commit comments

Comments
 (0)