From 1ad42135ef7b6efdc0e352f346acb951f9a93e28 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Wed, 17 Nov 2021 14:13:29 -0500 Subject: [PATCH] feat(types): Add proper dynamic function component type (#37) --- src/types.ts | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/types.ts b/src/types.ts index 1108e08..13c70ff 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,7 +4,7 @@ export type EventKey = string | number; export type IntrinsicElementTypes = keyof JSX.IntrinsicElements; -export type AssignProps< +export type AssignPropsWithRef< Inner extends string | React.ComponentType, P, > = Omit< @@ -13,12 +13,39 @@ export type AssignProps< > & P; +export type { AssignPropsWithRef as AssignProps }; + +export type AssignPropsWithoutRef< + Inner extends string | React.ComponentType, + P, +> = Omit< + React.ComponentPropsWithoutRef< + Inner extends React.ElementType ? Inner : never + >, + keyof P +> & + P; + export interface DynamicRefForwardingComponent< TInitial extends string | React.ComponentType, - P = unknown, + P = { children?: React.ReactNode }, +> { + = TInitial>( + props: AssignPropsWithRef, + context?: any, + ): React.ReactElement | null; + propTypes?: any; + contextTypes?: any; + defaultProps?: Partial

; + displayName?: string; +} + +export interface DynamicFunctionComponent< + TInitial extends string | React.ComponentType, + P = { children?: React.ReactNode }, > { = TInitial>( - props: React.PropsWithChildren>, + props: AssignPropsWithoutRef, context?: any, ): React.ReactElement | null; propTypes?: any; @@ -30,13 +57,13 @@ export interface DynamicRefForwardingComponent< export class DynamicComponent< As extends string | React.ComponentType, P = unknown, -> extends React.Component> {} +> extends React.Component> {} // Need to use this instead of typeof Component to get proper type checking. export type DynamicComponentClass< As extends string | React.ComponentType, P = unknown, -> = React.ComponentClass>; +> = React.ComponentClass>; export type SelectCallback = ( eventKey: string | null,