Skip to content

ReactiveForms: FormArray.at could return undefined #16933

@snerks

Description

@snerks

I'm submitting a ... (check one with "x")

[ x ] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

The FormArray.at method signature declares the return value to be AbstractControl.

Expected behavior

The FormArray.at method signature declares the return value to be AbstractControl | undefined.

Since:
FormArray.at returns the result of accessing the contained controls array.
undefined is returned when the supplied index is out-of-range, with respect to the target FormArray.

Note: Typescript does not point out an issue, even with strictNullChecks: true.

Minimal reproduction of the problem with instructions

    const formBuilder: FormBuilder = new FormBuilder();
    const formArray: FormArray = formBuilder.array([]);

    const outOfRangeItemPositiveAt: AbstractControl = formArray.at(5);
    const outOfRangeItemNegativeAt: AbstractControl = formArray.at(-1);

    console.log(`outOfRangeItemPositiveAt is defined = [${!!outOfRangeItemPositiveAt}]`);
    console.log(`outOfRangeItemNegativeAt is defined = [${!!outOfRangeItemNegativeAt}]`);

    console.log(`outOfRangeItemPositiveAt = [${outOfRangeItemPositiveAt}]`);
    console.log(`outOfRangeItemNegativeAt = [${outOfRangeItemNegativeAt}]`);

What is the motivation / use case for changing the behavior?

To indicate the possibility of an undefined result.

Please tell us about your environment:

  • Angular version: 4.1.3
  • Browser: [ Chrome ]
  • Language: [ TypeScript 2.3.2 | ES5 ]

  • Node (for AoT issues): node --version = v6.9.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions