Skip to content

Commit

Permalink
fix memoWithAs types
Browse files Browse the repository at this point in the history
  • Loading branch information
Chance Strickland committed Jun 24, 2020
1 parent 22b51e8 commit 82779c2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
12 changes: 7 additions & 5 deletions packages/utils/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
ComponentWithAs,
ComponentWithForwardedRef,
DistributiveOmit,
ElementByTag,
ElementTagNameMap,
ForwardRefExoticComponentWithAs,
ForwardRefWithAsRenderFunction,
FunctionComponentWithAs,
MemoExoticComponentWithAs,
PropsFromAs,
Expand Down Expand Up @@ -203,12 +206,9 @@ export function createNamedContext<ContextValueType>(
* the time time being.
*/
export function forwardRefWithAs<Props, ComponentType extends As = "div">(
render: (
props: PropsFromAs<ComponentType, Props>,
ref: React.RefObject<any>
) => React.ReactElement | null
render: ForwardRefWithAsRenderFunction<ComponentType, Props>
) {
return React.forwardRef(render as any) as ForwardRefExoticComponentWithAs<
return React.forwardRef(render) as ForwardRefExoticComponentWithAs<
ComponentType,
Props
>;
Expand Down Expand Up @@ -708,6 +708,8 @@ export {
ComponentWithAs,
ComponentWithForwardedRef,
DistributiveOmit,
ElementByTag,
ElementTagNameMap,
ForwardRefExoticComponentWithAs,
FunctionComponentWithAs,
MemoExoticComponentWithAs,
Expand Down
49 changes: 48 additions & 1 deletion packages/utils/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,56 @@ export interface MemoExoticComponentWithAs<
ComponentType extends As,
ComponentProps
> extends NamedExoticComponentWithAs<ComponentType, ComponentProps> {
readonly type: ComponentType;
readonly type: ComponentType extends React.ComponentType
? ComponentType
: FunctionComponentWithAs<ComponentType, ComponentProps>;
}

export interface ForwardRefWithAsRenderFunction<
ComponentType extends As,
ComponentProps = {}
> {
(
props: React.PropsWithChildren<PropsFromAs<ComponentType, ComponentProps>>,
ref:
| ((
instance:
| (ComponentType extends keyof ElementTagNameMap
? ElementByTag<ComponentType>
: any)
| null
) => void)
| React.MutableRefObject<
| (ComponentType extends keyof ElementTagNameMap
? ElementByTag<ComponentType>
: any)
| null
>
| null
): React.ReactElement | null;
displayName?: string;
// explicit rejected with `never` required due to
// https://github.com/microsoft/TypeScript/issues/36826
/**
* defaultProps are not supported on render functions
*/
defaultProps?: never;
/**
* propTypes are not supported on render functions
*/
propTypes?: never;
}

export type ElementTagNameMap = HTMLElementTagNameMap &
Pick<
SVGElementTagNameMap,
Exclude<keyof SVGElementTagNameMap, keyof HTMLElementTagNameMap>
>;

export type ElementByTag<
TagName extends keyof ElementTagNameMap
> = ElementTagNameMap[TagName];

/*
Test components to make sure our dynamic As prop components work as intended
Expand Down

0 comments on commit 82779c2

Please sign in to comment.