Skip to content

Commit

Permalink
fix(forms): fix FormRecord type inference (#50750)
Browse files Browse the repository at this point in the history
Updates type inference in `ɵElement` to make `FormRecord` take precedence over `FormGroup`

PR Close #50750
  • Loading branch information
EmmanuelRoux authored and AndrewKushnir committed Oct 22, 2024
1 parent 9762b24 commit 18b6f33
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions packages/forms/src/form_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,28 @@ export type ɵElement<T, N extends null> =
// through the distributive conditional type. This is the officially recommended solution:
// https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
//
// Note: Because `FormRecord` implementation extends `FormGroup`, it must be checked BEFORE `FormGroup`
// in the following clauses (otherwise it may incorrectly be inferred to `FormGroup`).
//
//
// Identify FormControl container types.
[T] extends [FormControl<infer U>]
? FormControl<U>
: // Or FormControl containers that are optional in their parent group.
[T] extends [FormControl<infer U> | undefined]
? FormControl<U>
: // FormGroup containers.
[T] extends [FormGroup<infer U>]
? FormGroup<U>
: // Optional FormGroup containers.
[T] extends [FormGroup<infer U> | undefined]
? FormGroup<U>
: // FormRecord containers.
[T] extends [FormRecord<infer U>]
? FormRecord<U>
: // Optional FormRecord containers.
[T] extends [FormRecord<infer U> | undefined]
? FormRecord<U>
: // FormRecord containers.
[T] extends [FormRecord<infer U>]
? FormRecord<U>
: // Optional FormRecord containers.
[T] extends [FormRecord<infer U> | undefined]
? FormRecord<U>
: // FormGroup containers.
[T] extends [FormGroup<infer U>]
? FormGroup<U>
: // Optional FormGroup containers.
[T] extends [FormGroup<infer U> | undefined]
? FormGroup<U>
: // FormArray containers.
[T] extends [FormArray<infer U>]
? FormArray<U>
Expand Down

0 comments on commit 18b6f33

Please sign in to comment.