diff --git a/lib/react/src/jsx-runtime/index.ts b/lib/react/src/jsx-runtime/index.ts index 6c03b03..e3c78da 100644 --- a/lib/react/src/jsx-runtime/index.ts +++ b/lib/react/src/jsx-runtime/index.ts @@ -1,28 +1,28 @@ import { jPatches } from './_internal' -import type { ComponentProps, ElementType, Key, ReactElement } from 'react' +import type { ElementType, Key, ReactElement } from 'react' import type { ReactJSXRuntime } from '..' export type AnyJSXFactoryFunction = (typeof ReactJSXRuntime)['jsx' | 'jsxs'] -export type BeforeJSXCallback< - E extends ElementType = ElementType, - P = ComponentProps, -> = ( - args: [element: E, props: P, key?: Key | undefined], +export type BeforeJSXCallback = ( + args: [element: E, props: ElementTypeProps, key?: Key | undefined], ) => Parameters -export type InsteadJSXCallback< - E extends ElementType = ElementType, - P = ComponentProps, -> = ( - args: [element: E, props: P, key?: Key | undefined], +export type InsteadJSXCallback = ( + args: [element: E, props: ElementTypeProps, key?: Key | undefined], jsx: AnyJSXFactoryFunction, ) => ReturnType | null -export type AfterJSXCallback = ( - fiber: ReactElement, +export type AfterJSXCallback = ( + fiber: ReactElement, E>, ) => ReturnType | null +type ElementTypeProps = E extends ElementType< + infer Props +> + ? Props + : never + /** * Registers a hook to be called after a JSX element with the specified type is created. * @@ -30,7 +30,10 @@ export type AfterJSXCallback = ( * @param patch The hook. * @returns A function to unpatch. */ -export function afterJSX(type: ElementType, patch: AfterJSXCallback) { +export function afterJSX( + type: E, + patch: AfterJSXCallback, +) { let patches = jPatches.get(type) if (!patches) { patches = [undefined, new Set(), undefined] @@ -39,9 +42,9 @@ export function afterJSX(type: ElementType, patch: AfterJSXCallback) { const set = patches[1]! - set.add(patch as AfterJSXCallback) + set.add(patch as AfterJSXCallback) return () => { - const res = set.delete(patch as AfterJSXCallback) + const res = set.delete(patch as AfterJSXCallback) if (res) attemptCleanup(type) return res } @@ -54,10 +57,10 @@ export function afterJSX(type: ElementType, patch: AfterJSXCallback) { * @param patch The hook. * @returns A function to unpatch. */ -export function beforeJSX< - E extends ElementType = ElementType, - P = ComponentProps, ->(type: E, patch: BeforeJSXCallback) { +export function beforeJSX( + type: E, + patch: BeforeJSXCallback, +) { let patches = jPatches.get(type) if (!patches) { patches = [new Set(), undefined, undefined] @@ -66,9 +69,9 @@ export function beforeJSX< const set = patches[0]! - set.add(patch as BeforeJSXCallback) + set.add(patch as BeforeJSXCallback) return () => { - const res = set.delete(patch as BeforeJSXCallback) + const res = set.delete(patch as BeforeJSXCallback) if (res) attemptCleanup(type) return res } @@ -81,10 +84,10 @@ export function beforeJSX< * @param patch The hook. * @returns A function to unpatch. */ -export function insteadJSX< - E extends ElementType = ElementType, - P = ComponentProps, ->(type: E, patch: InsteadJSXCallback) { +export function insteadJSX( + type: E, + patch: InsteadJSXCallback, +) { let patches = jPatches.get(type) if (!patches) { patches = [undefined, undefined, new Set()] @@ -93,9 +96,9 @@ export function insteadJSX< const set = patches[2]! - set.add(patch as InsteadJSXCallback) + set.add(patch as InsteadJSXCallback) return () => { - const res = set.delete(patch as InsteadJSXCallback) + const res = set.delete(patch as InsteadJSXCallback) if (res) attemptCleanup(type) return res }