Skip to content

Commit 1ad4213

Browse files
authored
feat(types): Add proper dynamic function component type (#37)
1 parent 7660dd4 commit 1ad4213

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/types.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type EventKey = string | number;
44

55
export type IntrinsicElementTypes = keyof JSX.IntrinsicElements;
66

7-
export type AssignProps<
7+
export type AssignPropsWithRef<
88
Inner extends string | React.ComponentType<any>,
99
P,
1010
> = Omit<
@@ -13,12 +13,39 @@ export type AssignProps<
1313
> &
1414
P;
1515

16+
export type { AssignPropsWithRef as AssignProps };
17+
18+
export type AssignPropsWithoutRef<
19+
Inner extends string | React.ComponentType<any>,
20+
P,
21+
> = Omit<
22+
React.ComponentPropsWithoutRef<
23+
Inner extends React.ElementType ? Inner : never
24+
>,
25+
keyof P
26+
> &
27+
P;
28+
1629
export interface DynamicRefForwardingComponent<
1730
TInitial extends string | React.ComponentType<any>,
18-
P = unknown,
31+
P = { children?: React.ReactNode },
32+
> {
33+
<As extends string | React.ComponentType<any> = TInitial>(
34+
props: AssignPropsWithRef<As, { as?: As } & P>,
35+
context?: any,
36+
): React.ReactElement | null;
37+
propTypes?: any;
38+
contextTypes?: any;
39+
defaultProps?: Partial<P>;
40+
displayName?: string;
41+
}
42+
43+
export interface DynamicFunctionComponent<
44+
TInitial extends string | React.ComponentType<any>,
45+
P = { children?: React.ReactNode },
1946
> {
2047
<As extends string | React.ComponentType<any> = TInitial>(
21-
props: React.PropsWithChildren<AssignProps<As, { as?: As } & P>>,
48+
props: AssignPropsWithoutRef<As, { as?: As } & P>,
2249
context?: any,
2350
): React.ReactElement | null;
2451
propTypes?: any;
@@ -30,13 +57,13 @@ export interface DynamicRefForwardingComponent<
3057
export class DynamicComponent<
3158
As extends string | React.ComponentType<any>,
3259
P = unknown,
33-
> extends React.Component<AssignProps<As, { as?: As } & P>> {}
60+
> extends React.Component<AssignPropsWithRef<As, { as?: As } & P>> {}
3461

3562
// Need to use this instead of typeof Component to get proper type checking.
3663
export type DynamicComponentClass<
3764
As extends string | React.ComponentType<any>,
3865
P = unknown,
39-
> = React.ComponentClass<AssignProps<As, { as?: As } & P>>;
66+
> = React.ComponentClass<AssignPropsWithRef<As, { as?: As } & P>>;
4067

4168
export type SelectCallback = (
4269
eventKey: string | null,

0 commit comments

Comments
 (0)