Which @angular/* package(s) are relevant/related to the feature request?
forms
Description
Due to their current type signatures, the new signal form min and max validators can only be applied to field paths that represent pure number values.
|
export function min<TPathKind extends PathKind = PathKind.Root>( |
|
path: FieldPath<number, TPathKind>, |
|
minValue: number | LogicFn<number, number | undefined, TPathKind>, |
|
config?: BaseValidatorConfig<number, TPathKind>, |
|
) { |
|
export function max<TPathKind extends PathKind = PathKind.Root>( |
|
path: FieldPath<number, TPathKind>, |
|
maxValue: number | LogicFn<number, number | undefined, TPathKind>, |
|
config?: BaseValidatorConfig<number, TPathKind>, |
|
) { |
For example, the min-validator cannot be applied to a number field of value type number | null, which represents the empty state with null instead of NaN for better type ergonomics:
class Component {
readonly field = form(signal<number|null>(null), (path) => { min(path, 1) });
}
produces the following type error:
Argument of type '{ [ɵɵTYPE]: [number | null, Child]; }' is not assignable to parameter of type '{ [ɵɵTYPE]: [number, Child]; }'.
Note that similar considerations apply to minLength and maxLength that might also want to support nullable field values.
Proposed solution
Relax the type signature of min and max to allow the FieldPath value types to include null.
As the validation logic is already not applied when the value is considered empty, it can remain unchanged.
|
if (isEmpty(ctx.value())) { |
|
return undefined; |
|
} |
Alternatives considered
One can use NaN to represent empty number inputs.
Which @angular/* package(s) are relevant/related to the feature request?
forms
Description
Due to their current type signatures, the new signal form
minandmaxvalidators can only be applied to field paths that represent pure number values.angular/packages/forms/signals/src/api/validators/min.ts
Lines 31 to 35 in 85994fb
angular/packages/forms/signals/src/api/validators/max.ts
Lines 31 to 35 in 85994fb
For example, the min-validator cannot be applied to a number field of value type
number | null, which represents the empty state withnullinstead ofNaNfor better type ergonomics:produces the following type error:
Note that similar considerations apply to
minLengthandmaxLengththat might also want to support nullable field values.Proposed solution
Relax the type signature of
minandmaxto allow theFieldPathvalue types to includenull.As the validation logic is already not applied when the value is considered empty, it can remain unchanged.
angular/packages/forms/signals/src/api/validators/min.ts
Lines 41 to 43 in 85994fb
Alternatives considered
One can use
NaNto represent empty number inputs.