Skip to content

Commit 63a8e65

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 63a8e65

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,32 @@ const everyHookRef = React.createRef<{ id: number }>();
263263
}}/>;
264264

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

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

0 commit comments

Comments
 (0)