Skip to content

Incorrect type inference for FormRecord #50751

Open

Description

@EmmanuelRoux

Which @angular/* package(s) are the source of the bug?

forms

Is this a regression?

No

Description

FormRecord is sometimes incorrectly inferred to be a FormGroup.

This is apparently caused by:

  • FormRecord class actually extends FormGroup
  • The way ɵElement is implemented (first checks for instance of FormGroup, before FormRecord)

Please provide a link to a minimal reproduction of the bug

This appeared as a TS error while working on #50750 (which adds a new method on FormRecord)
https://github.com/EmmanuelRoux/angular/tree/formrecord-clear

Revealed by some Angular's own tests, such as this one (extract below):

        const c = fb.array([fb.record({bar: 'foo'})]);
        {
          type ControlsType = Array<FormRecord<FormControl<string|null>>>;
          let t: ControlsType = c.controls; // <-- incorrectly inferred as controls of `FormGroup` instead of `FormRecord`
          let t1 = c.controls;
          t1 = null as unknown as ControlsType;
        }

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Working on PR #50750 which adds a new method on `FormRecord` (but not on `FormGroup`)

Angular CLI: 16.0.0-rc.4
Node: 20.3.0 (Unsupported)
Package Manager: yarn 1.22.19
OS: darwin arm64

Angular: undefined
... 

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1600.0-rc.4
@angular-devkit/build-angular   16.0.0-rc.4
@angular-devkit/core            16.0.0-rc.4
@angular-devkit/schematics      16.0.0-rc.4
@angular/build-tooling          0.0.0-5f06c4774df908ed69e1441f4ec63b898acf0c68
@angular/cdk                    16.0.0-rc.2
@angular/cli                    16.0.0-rc.4
@angular/material               16.0.0-rc.2
@angular/ng-dev                 <error>
@bazel/bazelisk                 1.12.1
@bazel/buildifier               6.1.2
@bazel/concatjs                 5.8.1
@bazel/esbuild                  5.8.1
@bazel/ibazel                   v0.16.2
@bazel/jasmine                  5.8.1
@bazel/protractor               5.8.1
@bazel/rollup                   5.8.1
@bazel/runfiles                 5.8.1
@bazel/terser                   5.8.1
@bazel/worker                   5.8.1
@schematics/angular             16.0.0-rc.4
rxjs                            6.6.7
typescript                      5.1.3

Anything else?

No response

Activity

  1. added this to the needsTriage milestone on Jun 19, 2023
  2. roger6106 commented on Feb 22, 2024

    @roger6106

    I experienced this issue, which presented itself as not being able to remove FormControls from a FormRecord. I ended up finding this issue after seeing a question posted on Stack Overflow (along with the needed solution).

  3. degloman commented on May 31, 2024

    @degloman

    I have the same issue, managed to fix it by replacing fb.group to new FormGroup()
    image

  4. EmmanuelRoux commented on May 31, 2024

    @EmmanuelRoux
    ContributorAuthor

    Hi, I think this will eventually be fixed by #50750

  5. roger6106 commented on May 28, 2025

    @roger6106

    It looks like that pull request was cancelled, and I can't find any sign of this issue being fixed anywhere else.

  6. EmmanuelRoux commented on May 28, 2025

    @EmmanuelRoux
    ContributorAuthor

    Both PR have been cancelled by Angular team

  7. JeanMeche commented on May 28, 2025

    @JeanMeche
    Member

    There was a rebase issue, if you still have the branch locally you should be able to force-push / reopen a PR.

  8. arturovt commented on Sep 29, 2026

    @arturovt
    Contributor

    @JeanMeche this can be closed unless the type fix is wanted. The cause is branch order in ɵElement: FormGroup is checked before FormRecord, and FormRecord extends FormGroup, so a record always matches the group branch first. Moving the FormRecord branches above the FormGroup ones would fix it and is type-only.

    It is still a compile-time behavior change in a maintained package, on a report with no duplicates since 2023, so it needs a presubmit to be worth it.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions