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.

Possible issue with UnpackNestedValue

See original GitHub issue

Describe the bug I’m sorry if this is a bug/mistake on my side. I’m still catching up with TS and react-hook-form Seems that certain types are not handled well with UnpackNestedValue

To Reproduce Steps to reproduce the behavior:

import { useForm } from "react-hook-form";
import "./styles.css";

const testFunction = (query: Record<string, string | undefined | null>): void => {
  console.log(query)
};

export const Second = <T extends Record<string, string | undefined | null>>(): void => {
  const form = useForm<T>();

  const values = form.watch();
  testFunction(values)
}

Error:

Argument of type 'UnpackNestedValue<T>' is not assignable to parameter of type 'Record<string, string | null | undefined>'.
  Type 'NonUndefined<T> | (NonUndefined<T> extends Date | FileList ? T : NonUndefined<T> extends Record<string, unknown> ? { [K in keyof T]: UnpackNestedValue<...>; } : T)' is not assignable to type 'Record<string, string | null | undefined>'.
    Type 'NonUndefined<T> extends Date | FileList ? T : NonUndefined<T> extends Record<string, unknown> ? { [K in keyof T]: UnpackNestedValue<T[K]>; } : T' is not assignable to type 'Record<string, string | null | undefined>'.
      Type 'T | (NonUndefined<T> extends Record<string, unknown> ? { [K in keyof T]: UnpackNestedValue<T[K]>; } : T)' is not assignable to type 'Record<string, string | null | undefined>'.
        Type 'NonUndefined<T> extends Record<string, unknown> ? { [K in keyof T]: UnpackNestedValue<T[K]>; } : T' is not assignable to type 'Record<string, string | null | undefined>'.
          Type 'T | { [K in keyof T]: UnpackNestedValue<T[K]>; }' is not assignable to type 'Record<string, string | null | undefined>'.
            Type '{ [K in keyof T]: UnpackNestedValue<T[K]>; }' is not assignable to type 'Record<string, string | null | undefined>'.
              Type 'UnpackNestedValue<T[K]>' is not assignable to type 'string | null | undefined'.
                Type 'Record<string, unknown> | unknown[] | (NonUndefined<T[K]> extends Date | FileList ? T[K] : NonUndefined<T[K]> extends Record<string, unknown> ? { [K in keyof T[K]]: UnpackNestedValue<...>; } : T[K])' is not assignable to type 'string | null | undefined'.
                  Type 'Record<string, unknown>' is not assignable to type 'string'.
                    Type 'UnpackNestedValue<T[K]>' is not assignable to type 'string'.
                      Type 'Record<string, unknown> | unknown[] | (NonUndefined<T[K]> extends Date | FileList ? T[K] : NonUndefined<T[K]> extends Record<string, unknown> ? { [K in keyof T[K]]: UnpackNestedValue<...>; } : T[K])' is not assignable to type 'string'.
                        Type 'Record<string, unknown>' is not assignable to type 'string'.

Expected behavior

if I tweak UnpackNestedValue a bit error is gone.

export declare type UnpackNestedValue<T> = NonUndefined<T> extends NestedValue<infer U>
    ? U
    : NonUndefined<T> extends Date | FileList
        ? T
        : T extends Record<string, unknown> // change NonUndefined<T> to T
            ? {
                [K in keyof T]: UnpackNestedValue<T[K]>;
            }
            : T;

Additional context issue arrived with 6.x react-hook-form version

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

15reactions
parker45commented, May 9, 2022

+1 to reopen this!

9reactions
k-one-o-twocommented, Dec 28, 2021

I’m using ^7.20.2 and facing the same error, can this please be reopened?

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hook Form Type Issue with EditorState ...
I am currently wondering how the UnpackNestedValues Type works for objects like Files (Blob) or an EditorState (DraftJS).
Read more >
TS Support | React Hook Form - Simple React forms validation
This type will return union with all available paths that match the passed value. Copy. function Field< TFieldValues extends FieldValues, TPath extends ...
Read more >
defaultValues-typing-UnpackNestedValue - CodeSandbox
VS Code's tsserver was deleted by another application such as a misbehaving virus detection tool. Please reinstall VS Code. Manage Extension.
Read more >
How to create reusable form components with React Hook ...
When we attempt to do this, we'll encounter an issue. ... Each form will likely have different validation rules and errors. Now what?...
Read more >
react-hook-form - Awesome JS
promise will only be resolved once during useForm() call; It's possible to ... fix #9571 validation issue with unregister input with valueAsNumber (#9572)...
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