Skip to content

fix(forms): return a numeric keyInParent for array items - #71617

Open
fabioemoutinho wants to merge 1 commit into
angular:mainfrom
fabioemoutinho:fix/forms-key-in-parent-number
Open

fabioemoutinho wants to merge 1 commit into
angular:mainfrom
fabioemoutinho:fix/forms-key-in-parent-number

Conversation

@fabioemoutinho

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

FieldState.keyInParent() is typed Signal<TKey> with TKey = number for children of an array field, and documented as the index in that array, but at runtime it returns the string property key ('1'), since child keys come from Object.keys(). Code like i !== item().keyInParent() type-checks but never matches.

Issue Number: #71616

What is the new behavior?

For children of an array field, keyInParent() returns the index as a number, matching the declared type and the JSDoc. Object property keys are still strings. The internal string keys (used for pathKeys and FieldContext.key) are unchanged; this follows the same conversion FieldContext.index already does.

The issue also mentions the other option (changing the type to string), in case that's preferred.

Does this PR introduce a breaking change?

  • Yes
  • No

Code that relied on the undocumented string value (e.g. === '1') would see a number instead, but that contradicts the declared type.

Other information

Fixes #71616

`FieldState.keyInParent` is typed as `number` for children of an array
field and documented as the index in that array, but it returned the
string property key (e.g. `'1'`) because child keys come from
`Object.keys()`. Comparisons like `i !== item().keyInParent()` then
type-check but never match.

Convert the key to a number when the parent value is an array. Object
property keys are unchanged, as are the internal string keys used for
`pathKeys` and `FieldContext.key`.

Fixes angular#71616
@ngbot ngbot Bot added this to the Backlog milestone Oct 26, 2026
@pullapprove
pullapprove Bot requested a review from crisbeto October 26, 2026 01:53
@fabioemoutinho

Copy link
Copy Markdown
Contributor Author

@alxhub I found your commit bb6d010 ("align runtime type of keyInParent with TypeScript definition", for #67241) on your fork after opening this. It fixes the same bug at the source, in createKeyInParent, and also updates the context key types, while this PR only converts in the public keyInParent getter.

I don't see it in a PR yet. If you plan to land that version, I'm happy to close this one. Otherwise, let me know if you'd like this PR changed to match your approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

signal forms: keyInParent() returns a string for array items (typed as number)

1 participant