Skip to content

Commit

Permalink
Callback prop type inference fix (#64)
Browse files Browse the repository at this point in the history
* Fix for callback prop type inference.

* add changeset

* Documentation fixes.
  • Loading branch information
nsaunders authored Jul 13, 2024
1 parent 831d97e commit 096c9e9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-sheep-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@embellish/react": patch
---

Type inference fix for callback props.
18 changes: 18 additions & 0 deletions docs/api/react.callbackpropfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@embellish/react](./react.md) &gt; [CallbackPropFix](./react.callbackpropfix.md)

## CallbackPropFix type

Fixes type inference for callback props, e.g. `onClick`<!-- -->.

**Signature:**

```typescript
export declare type CallbackPropFix<T> = T extends any ? T : never;
```

## Remarks

See [microsoft/TypeScript\#44596 (comment)](https://github.com/microsoft/TypeScript/issues/44596#issuecomment-1447648600)<!-- -->.

6 changes: 4 additions & 2 deletions docs/api/react.componentprops.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ Partial<{
? P[PropName]
: never;
}>,
> = Omit<
> = CallbackPropFix<
Omit<
JSX.LibraryManagedAttributes<Is, ComponentPropsWithRef<Is>>,
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)

11 changes: 11 additions & 0 deletions docs/api/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ Description
Creates a branded type to add a unique symbol for type differentiation.


</td></tr>
<tr><td>

[CallbackPropFix](./react.callbackpropfix.md)


</td><td>

Fixes type inference for callback props, e.g. `onClick`<!-- -->.


</td></tr>
<tr><td>

Expand Down
19 changes: 16 additions & 3 deletions packages/react/src/component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ export interface ComponentOptions<P, C extends string, DefaultIs> {
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> = T extends any ? T : never;

/**
* Component props
*
Expand Down Expand Up @@ -84,9 +95,11 @@ export type ComponentProps<
? P[PropName]
: never;
}>,
> = Omit<
JSX.LibraryManagedAttributes<Is, ComponentPropsWithRef<Is>>,
keyof OwnProps
> = CallbackPropFix<
Omit<
JSX.LibraryManagedAttributes<Is, ComponentPropsWithRef<Is>>,
keyof OwnProps
>
> &
OwnProps;

Expand Down
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type {
ValidConditionName,
} from "@embellish/core";
export type {
CallbackPropFix,
Component,
ComponentProps,
ComponentOptions,
Expand Down

0 comments on commit 096c9e9

Please sign in to comment.