`T extends string` is causing type check errors for setValue
See original GitHub issueDescribe the bug
if we have type T extends string
for value then setValue(name, value);
is giving Type error:
TS2345: Argument of type 'T | undefined' is not assignable to parameter of type '(T extends NestedValue<infer U> ? U : UnpackNestedValue<DeepPartial<LiteralToPrimitive<T>>>) | undefined'.
Type 'T' is not assignable to type '(T extends NestedValue<infer U> ? U : UnpackNestedValue<DeepPartial<LiteralToPrimitive<T>>>) | undefined'.
Type 'string' is not assignable to type '(T extends NestedValue<infer U> ? U : UnpackNestedValue<DeepPartial<LiteralToPrimitive<T>>>) | undefined'.
Type 'T' is not assignable to type 'T extends NestedValue<infer U> ? U : UnpackNestedValue<DeepPartial<LiteralToPrimitive<T>>>'.
Type 'string' is not assignable to type 'T extends NestedValue<infer U> ? U : UnpackNestedValue<DeepPartial<LiteralToPrimitive<T>>>'.
seems that as soon as you extend on primitive type you go down the UnpackNestedValue
here:
TFieldValue extends NestedValue<infer U>
? U
: UnpackNestedValue<DeepPartial<LiteralToPrimitive<TFieldValue>>>
Why you don’t change implementation to?
setValue<TFieldName extends string, TFieldValue extends TFieldValues[TFieldName]>(
name: TFieldName,
value: TFieldValue,
options?: SetValueConfig,
): void;
To Reproduce try this example:
import {useFormContext} from 'react-hook-form';
type SelectFieldProps<T> = {
name: string;
value?: T;
};
export function SelectField<T extends string>({name, value}: SelectFieldProps<T>): void {
const {setValue} = useFormContext();
useEffect(() => {
setValue(name, value);
}, [name, setValue, value]);
}
Expected behavior I don’t expect any type errors here
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Java generics - assigning Object to a typed value
Your objective, I have to presume, is to detect type errors, where the value found in valueByCode doesn't match the generic type of...
Read more >TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >Documentation - TypeScript 2.0
A property access or a function call produces a compile-time error if the object or function is of a type that includes null...
Read more >FormGroup - Angular
registerControl<K extends string & keyof TControl>(name: K, ... Error When strict checks fail, such as setting the value of a control that doesn't...
Read more >useForm - setValue - React Hook Form
const { replace } = useFieldArray({ name: 'test' }) // ❌ doesn't create new input ... When setValue cause state update, such as...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
🙏 thank you for fixing it!
https://codesandbox.io/s/react-hook-form-useform-template-forked-2zdxm?file=/src/demo.ts