question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`T extends string` is causing type check errors for setValue

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found