diff --git a/.changeset/dull-sheep-hug.md b/.changeset/dull-sheep-hug.md new file mode 100644 index 0000000..37d8718 --- /dev/null +++ b/.changeset/dull-sheep-hug.md @@ -0,0 +1,5 @@ +--- +"@embellish/react": patch +--- + +Type inference fix for callback props. diff --git a/docs/api/react.callbackpropfix.md b/docs/api/react.callbackpropfix.md new file mode 100644 index 0000000..d45611a --- /dev/null +++ b/docs/api/react.callbackpropfix.md @@ -0,0 +1,18 @@ + + +[Home](./index.md) > [@embellish/react](./react.md) > [CallbackPropFix](./react.callbackpropfix.md) + +## CallbackPropFix type + +Fixes type inference for callback props, e.g. `onClick`. + +**Signature:** + +```typescript +export declare type CallbackPropFix = T extends any ? T : never; +``` + +## Remarks + +See [microsoft/TypeScript\#44596 (comment)](https://github.com/microsoft/TypeScript/issues/44596#issuecomment-1447648600). + diff --git a/docs/api/react.componentprops.md b/docs/api/react.componentprops.md index 93129ff..6c87cb2 100644 --- a/docs/api/react.componentprops.md +++ b/docs/api/react.componentprops.md @@ -37,11 +37,13 @@ Partial<{ ? P[PropName] : never; }>, -> = Omit< +> = CallbackPropFix< +Omit< JSX.LibraryManagedAttributes>, keyof OwnProps +> > & OwnProps; ``` -**References:** [ValidConditionName](./react.validconditionname.md), [Condition](./react.condition.md), [ComponentPropsWithRef](./react.componentpropswithref.md) +**References:** [ValidConditionName](./react.validconditionname.md), [Condition](./react.condition.md), [CallbackPropFix](./react.callbackpropfix.md), [ComponentPropsWithRef](./react.componentpropswithref.md) diff --git a/docs/api/react.md b/docs/api/react.md index e5d2704..bb9600a 100644 --- a/docs/api/react.md +++ b/docs/api/react.md @@ -123,6 +123,17 @@ Description Creates a branded type to add a unique symbol for type differentiation. + + + +[CallbackPropFix](./react.callbackpropfix.md) + + + + +Fixes type inference for callback props, e.g. `onClick`. + + diff --git a/packages/react/src/component.d.ts b/packages/react/src/component.d.ts index e20411d..a63fc68 100644 --- a/packages/react/src/component.d.ts +++ b/packages/react/src/component.d.ts @@ -45,6 +45,17 @@ export interface ComponentOptions { fallback?: "revert-layer" | "unset"; } +/** + * Fixes type inference for callback props, e.g. `onClick`. + * + * @remarks + * See + * {@link https://github.com/microsoft/TypeScript/issues/44596#issuecomment-1447648600 | microsoft/TypeScript#44596 (comment)}. + * + * @public + */ +export type CallbackPropFix = T extends any ? T : never; + /** * Component props * @@ -84,9 +95,11 @@ export type ComponentProps< ? P[PropName] : never; }>, -> = Omit< - JSX.LibraryManagedAttributes>, - keyof OwnProps +> = CallbackPropFix< + Omit< + JSX.LibraryManagedAttributes>, + keyof OwnProps + > > & OwnProps; diff --git a/packages/react/src/index.ts b/packages/react/src/index.ts index 597efa7..60bd460 100644 --- a/packages/react/src/index.ts +++ b/packages/react/src/index.ts @@ -14,6 +14,7 @@ export type { ValidConditionName, } from "@embellish/core"; export type { + CallbackPropFix, Component, ComponentProps, ComponentOptions,