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.

Help: Typescript errors support for field arrays

See original GitHub issue

Describe the bug I have a form in which I dynamically create field arrays (as in the examples). Therefore I have the following Typescript interface:

export interface ICostInformationFormValues {
  rentalCost: number;
  additionalCost: Array<{ name: string; value: number }>;
}

I want to display validation errors for the dynamically generated fields and that’s the problem. The field arrays are handled differently in the errors than in for example getValues({ nested: true }). Instead of using my Typescript interface, the Field Array error messages are stored with the index of the field name (e.g.: “additionalCost[1][value]”).

When I try to read the errors for this field:

errors["additionalCost[1][value]"]

I get the following Typescript error message:

Element implicitly has an 'any' type because expression of type '"additionalCost[1][value]"' can't be used to index type 'Partial<Record<"rentalCost" | "additionalCost", FieldError>>'.
  Property 'additionalCost[1][value]' does not exist on type 'Partial<Record<"rentalCost" | "additionalCost", FieldError>>'.

When I simply log the errors value to my console I can see the error messages for the array fields. I just can’t read them with Typescript support.

Expected behavior I would expect to be able to read the error messages based on my Typescript interface like I read the errors for every other field.

errors.additionalCost[1]["value"].message

Additional context I’m using Material-UI to create my textfields. If you need more context please let me know.

Thanks for your help 😃 Daniel

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
stramelcommented, Oct 8, 2019

@bluebill1049 We just need to union it with a string. Change

https://github.com/react-hook-form/react-hook-form/blob/75d213c4fb6e686aad6befab8d9d917cd25ffd62/src/types.ts#L100

to

Record<keyof Data | string, FieldError>

1reaction
bluebill1049commented, Oct 9, 2019

@yeyep we going to release the patch soon

Read more comments on GitHub >

github_iconTop Results From Across the Web

Any does array.method get error on typescript? - Stack Overflow
I am converting couple of my javascript code into typescript in order to practice typescript. I am assuming I am doing everything correct...
Read more >
Documentation - Everyday Types - TypeScript
In this chapter, we'll cover some of the most common types of values you'll find in JavaScript code, and explain the corresponding ways...
Read more >
Destructuring assignment - JavaScript - MDN Web Docs
The rest property of array destructuring assignment can be another array or object binding pattern. This allows you to simultaneously unpack the ...
Read more >
Google TypeScript Style Guide
Tip: If you only need some of the elements from an array (or TypeScript ... and can help resolve confusion regarding observable values...
Read more >
TypeScript errors and how to fix them
error TS1218: Export assignment is not supported when '–module' flag is 'system'. ... that helps the TypeScript compiler to distinguish the different types: ......
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