Skip to content

🚏 feat: support optional array fields in PathValueImpl type#13057

Merged
bluebill1049 merged 5 commits intoreact-hook-form:masterfrom
MPrieur-chaps:support-optional-array-fields-in-PathValueImpl-type
Sep 20, 2025
Merged

🚏 feat: support optional array fields in PathValueImpl type#13057
bluebill1049 merged 5 commits intoreact-hook-form:masterfrom
MPrieur-chaps:support-optional-array-fields-in-PathValueImpl-type

Conversation

@MPrieur-chaps
Copy link
Copy Markdown
Contributor

When targeting an item of an optional array field, the PathValueImpl type omits the fact that the array is optional.

Given a type MyType, with an optional string array as a field:

type MyType = {
    optionalArray?: string[]
}
  • The returned type of PathValueImpl<MyType, `optionalArray.${number}`> should be string | undefined since optionalArray is possibly undefined. Using the current version, the returned type would be string.
  • Similarly, the returned type of PathValueImpl<string[] | undefined,`${number}`> should be string | undefined. Using the current version, the returned type would be string.

Given this, the following code should not get any Typescript errors:

const optionalArrayItem: PathValueImpl<MyType, `optionalArray.${number}`> = undefined

const optionalArrayItem2: PathValueImpl<string[] | undefined, `${number}`> = undefined

Here is a typescript playground comparing the results using current and new version of PathValueImpl.

I encountered this while using useWatch on my form containing an optional array field, noticing the returned type wasn't safe since it doesn't enforce handling the case where the result is undefined.

This PR:

  • updates the PathValueImpl type to take optional array fields into consideration
  • adds 4 tsd tests for PathValue, which uses PathValueImpl, to ensure the returned type extends undefined when targeting an optional array field
  • updates the Contributing documentation by replacing the tsd type tests command by test:type, as I noticed the command wasn't right while following the contributing guide.

By the way, I'm a developper using react-hook-form for professional purposes. I work at ChapsVision. I specifically created a professional GitHub account for this PR, that's why it is so young and empty.

@bluebill1049 bluebill1049 changed the title Support optional array fields in PathValueImpl type 🚏 feat: support optional array fields in PathValueImpl type Sep 20, 2025
@bluebill1049 bluebill1049 merged commit 8d61561 into react-hook-form:master Sep 20, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants