Skip to content

signal-forms: Relax typing for max and min validators to support impure number field values #63789

Description

@ThiloAschebrock

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.

Activity

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

Metadata

Metadata

Assignees

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions