Skip to content

Commit fa0c86e

Browse files
pierrezimmermannbampierrezimmermannmdjastrzebski
authored
fix: correct type inference for props when initialProps is used (#1421)
* fix: correct type inference for props when initialProps is used * Update src/__tests__/renderHook.test.tsx * refactor: simplify type of Renderhook options --------- Co-authored-by: pierrezimmermann <[email protected]> Co-authored-by: Maciej Jastrzebski <[email protected]>
1 parent 5bb5d2d commit fa0c86e

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/__tests__/renderHook.test.tsx

+34
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,37 @@ test('allows wrapper components', async () => {
6060

6161
expect(result.current).toEqual('provided');
6262
});
63+
64+
const useMyHook = (param: number | undefined) => {
65+
return param;
66+
};
67+
68+
test('props type is infered correctly when initial props is defined', () => {
69+
const { result, rerender } = renderHook(
70+
(num: number | undefined) => useMyHook(num),
71+
{
72+
initialProps: 5,
73+
}
74+
);
75+
76+
expect(result.current).toBe(5);
77+
78+
rerender(6);
79+
80+
expect(result.current).toBe(6);
81+
});
82+
83+
test('props type is inferred correctly when initial props is explicitly undefined', () => {
84+
const { result, rerender } = renderHook(
85+
(num: number | undefined) => useMyHook(num),
86+
{
87+
initialProps: undefined,
88+
}
89+
);
90+
91+
expect(result.current).toBeUndefined();
92+
93+
rerender(6);
94+
95+
expect(result.current).toBe(6);
96+
});

src/renderHook.tsx

+4-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@ export type RenderHookResult<Result, Props> = {
88
unmount: () => void;
99
};
1010

11-
export type RenderHookOptions<Props> = Props extends
12-
| object
13-
| string
14-
| number
15-
| boolean
16-
? {
17-
initialProps: Props;
18-
wrapper?: ComponentType<any>;
19-
}
20-
: { wrapper?: ComponentType<any>; initialProps?: never } | undefined;
11+
export type RenderHookOptions<Props> = {
12+
initialProps?: Props;
13+
wrapper?: ComponentType<any>;
14+
};
2115

2216
export function renderHook<Result, Props>(
2317
renderCallback: (props: Props) => Result,

0 commit comments

Comments
 (0)