Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.

Commit 6dc86d1

Browse files
committed
fix types for NestedValue type
1 parent 54333d0 commit 6dc86d1

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Control } from 'react-hook-form';
1+
import { NestedValue, Control } from 'react-hook-form';
22

33
export type ArrayWithLength<N extends number> = { [K in N]: any };
44

@@ -104,6 +104,14 @@ export type DeepPathValue<
104104
? TValues[TPath]
105105
: any;
106106

107+
export type NonUndefined<T> = T extends undefined ? never : T;
108+
109+
export type UnpackNestedValue<T> = NonUndefined<T> extends NestedValue<infer U>
110+
? U
111+
: NonUndefined<T> extends object
112+
? { [K in keyof T]: UnpackNestedValue<T[K]> }
113+
: T;
114+
107115
export type FieldValuesFromControl<
108116
TControl extends Control
109117
> = TControl extends Control<infer TFieldValues> ? TFieldValues : never;

src/useTypedController.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { Controller, Control } from 'react-hook-form';
33
import { formatName } from './logic/formatName';
44
import {
55
DeepPath,
6+
UnpackNestedValue,
67
FieldValuesFromControl,
78
Options,
89
ControllerProps,
910
} from './types';
1011

1112
export const useTypedController = <
12-
TFieldValues extends FieldValuesFromControl<TControl>,
13+
TFieldValues extends UnpackNestedValue<FieldValuesFromControl<TControl>>,
1314
TControl extends Control = Control
1415
>({
1516
control,

0 commit comments

Comments
 (0)