Skip to content

Commit 45887bf

Browse files
committed
fix: type def for forwardRef in injectIntl, fix #1444
1 parent e7bd24e commit 45887bf

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/components/injectIntl.tsx

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ const {Consumer: IntlConsumer, Provider: IntlProvider} = IntlContext;
2020
export const Provider = IntlProvider;
2121
export const Context = IntlContext;
2222

23-
export interface Opts<IntlPropName extends string = 'intl'> {
23+
export interface Opts<
24+
IntlPropName extends string = 'intl',
25+
ForwardRef extends boolean = false
26+
> {
2427
intlPropName?: IntlPropName;
25-
forwardRef?: boolean;
28+
forwardRef?: ForwardRef;
2629
enforceContext?: boolean;
2730
}
2831

@@ -35,12 +38,37 @@ export type WithIntlProps<P> = Omit<P, keyof WrappedComponentProps> & {
3538
};
3639

3740
export default function injectIntl<
38-
IntlPropName extends string = 'intl',
41+
IntlPropName extends string,
3942
P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>
4043
>(
4144
WrappedComponent: React.ComponentType<P>,
42-
options?: Opts<IntlPropName>
43-
): React.ComponentType<WithIntlProps<P>> & {
45+
options?: Opts<IntlPropName, false>
46+
): React.FC<WithIntlProps<P>> & {
47+
WrappedComponent: typeof WrappedComponent;
48+
};
49+
export default function injectIntl<
50+
IntlPropName extends string = 'intl',
51+
P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>,
52+
T extends React.ComponentType<P> = any
53+
>(
54+
WrappedComponent: React.ComponentType<P>,
55+
options?: Opts<IntlPropName, true>
56+
): React.ForwardRefExoticComponent<
57+
React.PropsWithoutRef<WithIntlProps<P>> & React.RefAttributes<T>
58+
> & {
59+
WrappedComponent: typeof WrappedComponent;
60+
};
61+
export default function injectIntl<
62+
IntlPropName extends string = 'intl',
63+
P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>,
64+
ForwardRef extends boolean = false,
65+
T extends React.ComponentType<P> = any
66+
>(
67+
WrappedComponent: React.ComponentType<P>,
68+
options?: Opts<IntlPropName, ForwardRef>
69+
): React.ForwardRefExoticComponent<
70+
React.PropsWithoutRef<WithIntlProps<P>> & React.RefAttributes<T>
71+
> & {
4472
WrappedComponent: typeof WrappedComponent;
4573
} {
4674
const {intlPropName = 'intl', forwardRef = false, enforceContext = true} =
@@ -72,7 +100,7 @@ export default function injectIntl<
72100

73101
if (forwardRef) {
74102
return hoistNonReactStatics(
75-
React.forwardRef((props: P, ref) => (
103+
React.forwardRef<T, P>((props: P, ref) => (
76104
<WithIntl {...props} forwardedRef={ref} />
77105
)),
78106
WrappedComponent

0 commit comments

Comments
 (0)