Skip to content

fix(forms): enforce nullable types for signal form number and date inputs - #72231

Open
JeanMeche wants to merge 1 commit into
angular:mainfrom
JeanMeche:fix/form-field-nullable-number-date
Open

JeanMeche wants to merge 1 commit into
angular:mainfrom
JeanMeche:fix/form-field-nullable-number-date

Conversation

@JeanMeche

Copy link
Copy Markdown
Member

Draft PR to test TGP impact of enforcing invariant nullable types on native numeric and date [formField] bindings.

Uses the same invariant structural union check in TcbNativeFieldOp that <input type="text"> already uses:

  • number, datetime-local: Field<string> | Field<number | null>
  • range: Field<string> | Field<number> | Field<number | null>
  • date, month, time, week: Field<string> | Field<number> | Field<Date | null>

Fixes #72230

@angular-robot angular-robot Bot added the area: compiler Issues related to `ngc`, Angular's template compiler label Nov 26, 2026
@ngbot ngbot Bot added this to the Backlog milestone Nov 26, 2026
…puts

Use an invariant structural union check in TcbNativeFieldOp for numeric and date inputs, matching the existing check used for text inputs.

Previously, these inputs used a covariant read-only check that allowed non-nullable Field<number> on <input type="number"> and Field<Date> on <input type="date">, even though clearing the input writes null to the model signal at runtime.

- number, datetime-local: string | number | null (invariant)
- range: string | number | number | null (invariant)
- date, month, time, week: string | number | Date | null (invariant)

Fixes angular#72230
@JeanMeche
JeanMeche force-pushed the fix/form-field-nullable-number-date branch from 8efa053 to 7bacdee Compare November 26, 2026 13:39
@JeanMeche JeanMeche changed the title fix(compiler): enforce nullable types for signal form number and date inputs fix(forms): enforce nullable types for signal form number and date inputs Nov 26, 2026
@JeanMeche
JeanMeche requested a review from leonsenft November 26, 2026 13:43
@JeanMeche
JeanMeche marked this pull request as ready for review November 26, 2026 13:53
@JeanMeche JeanMeche added the action: global presubmit The PR is in need of a google3 global presubmit label Nov 26, 2026
@SkyZeroZx

Copy link
Copy Markdown
Contributor

Just as a note the age/rating example is still using non nullable numbers and isn't covered by #72217

```angular-ts
import {Component, signal} from '@angular/core';
import {form, FormField, min, max} from '@angular/forms/signals';
@Component({
selector: 'app-age-form',
imports: [FormField],
template: `
<form novalidate>
<label>
Age
<input type="number" [formField]="ageForm.age" />
</label>
<label>
Rating (1-5)
<input type="number" [formField]="ageForm.rating" />
</label>
</form>
`,
})
export class AgeFormComponent {
ageModel = signal({
age: 0,
rating: 0,
});
ageForm = form(this.ageModel, (schemaPath) => {
min(schemaPath.age, 18, {message: 'You must be at least 18 years old'});
max(schemaPath.age, 120, {message: 'Please enter a valid age'});
min(schemaPath.rating, 1, {message: 'Rating must be at least 1'});
max(schemaPath.rating, 5, {message: 'Rating cannot exceed 5'});
});
}
```

I checked it against the base commit and this PR: it compiles before the change, but both [formField] bindings now fail with TS2322.

@MeAkib

MeAkib commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Just as a note the age/rating example is still using non nullable numbers and isn't covered by #72217

```angular-ts
import {Component, signal} from '@angular/core';
import {form, FormField, min, max} from '@angular/forms/signals';
@Component({
selector: 'app-age-form',
imports: [FormField],
template: `
<form novalidate>
<label>
Age
<input type="number" [formField]="ageForm.age" />
</label>
<label>
Rating (1-5)
<input type="number" [formField]="ageForm.rating" />
</label>
</form>
`,
})
export class AgeFormComponent {
ageModel = signal({
age: 0,
rating: 0,
});
ageForm = form(this.ageModel, (schemaPath) => {
min(schemaPath.age, 18, {message: 'You must be at least 18 years old'});
max(schemaPath.age, 120, {message: 'Please enter a valid age'});
min(schemaPath.rating, 1, {message: 'Rating must be at least 1'});
max(schemaPath.rating, 5, {message: 'Rating cannot exceed 5'});
});
}
```

I checked it against the base commit and this PR: it compiles before the change, but both [formField] bindings now fail with TS2322.

Good catch, thanks @SkyZeroZx. I checked the rest of the guides and found a few more snippets that bind a non-nullable number model to <input type="number">:

  • guide/forms/signals/validation.md: the age/rating min()/max() example
  • guide/forms/signals/form-logic.md: the Order example (twice), the Age example and the Inventory example
  • reference/errors/NG8022.md: both examples

Two of them also compare the value with < in a when condition, which then needs a null fallback such as (valueOf(p.age) ?? 0) < 18.

@JeanMeche I can send a follow-up docs PR for these once #72217 lands, unless you'd rather include them in this PR.

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

Labels

action: global presubmit The PR is in need of a google3 global presubmit area: compiler Issues related to `ngc`, Angular's template compiler area: forms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Signal Forms: [formField] should require nullable types on numeric and date inputs

3 participants