Skip to content

Support full structural subtyping/"duck typing" for typed forms #49374

@henry-alakazhang

Description

@henry-alakazhang

Which @angular/* package(s) are relevant/related to the feature request?

forms

Description

In TS you can take advantage of duck typing to assign objects that contain the full shape of another, for example:

value: { foo: string, bar: string };
subset: { foo: string };

subset = value;

This works through most generic types and classes as well (eg. a Promise<typeof value> can be used in place of a Promise<typeof subset>).

However, this doesn't work (most of the time) when assigning across FormGroups. This would be a useful feature for shareable/generic form components - they could take a FormGroup of a certain type, where some (or all) fields might be optional, but where the passing component can specify some of them.

Some examples are in this stackblitz: https://stackblitz.com/edit/angular-jgpu5s?file=src/main.ts

The error (specifically in the second example) suggests the type is forcing the assignability check in both directions regardless of which side is actually being assigned, while the two types are at least somewhat compatible (can be compared).

The only alternative at the moment is to use the least restrictive type for everything, which can add unneeded fields to the usages, and/or add unneeded and cumbersome optionality/nullability.

Wasn't sure whether to put this as a feature request or bug, since there might actually be a functional difference/design decision that I'm not understanding. Possibly (?) related to #47091 but that seemed a bit more specific and had some inheritence/extension complexity.

Proposed solution

Support this:

formGroup: FormGroup<{ foo: FormControl<string>; bar: FormControl<string> };
formGroupSubset: FormGroup<{ foo: FormControl<string> }>;

formGroupSubset = formGroup;

or at least this:

formGroup: FormGroup<{ foo: FormControl<string>; bar: FormControl<string> };
optionalFormGroup: FormGroup<{ foo?: FormControl<string>, bar?: FormControl<string> }>;

optionalFormGroup = formGroup;

Alternatives considered

N/A?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions