Sub topic 8
Strictly Typed Forms and Non-Nullable Controls
This chapter assumes you know TypeScript generics and interfaces, and that you have already built a FormControl and a FormGroup in earlier chapters. Since Angular 14, reactive forms are strictly typed by default. The compiler knows the value type of every control, so it can catch mistakes like assigning a number to a text field before the code runs.
How Angular Infers Control Types
A FormControl takes its type from its initial value. There is one catch. By default the type also includes null, because calling reset() sets the control back to null.
import { FormControl } from '@angular/forms';
const nickname = new FormControl('guest');
// Type: FormControl<string | null>
nickname.reset();
console.log(nickname.value); // null
const email = new FormControl('[email protected]', { nonNullable: true });
// Type: FormControl<string>
email.reset();
console.log(email.value); // '[email protected]'When you pass { nonNullable: true }, the control's initial value becomes its reset value, and null is removed from the type. For most text inputs this is the behavior you want.
Typing a FormGroup Explicitly
Inference is enough for small forms. If you want the form's shape documented in one place, declare an interface of controls and pass it as the generic argument. A field that can really be empty, such as an optional age, should say so with an explicit generic.
import { FormControl, FormGroup } from '@angular/forms';
interface ProfileForm {
name: FormControl<string>;
age: FormControl<number | null>;
}
const profile = new FormGroup<ProfileForm>({
name: new FormControl('', { nonNullable: true }),
age: new FormControl<number | null>(null),
});
profile.controls.name.setValue('Ada');
// profile.controls.name.setValue(42); // Compile error: number is not assignable to string
const partial = profile.value; // Partial<{ name: string; age: number | null }>
const full = profile.getRawValue(); // { name: string; age: number | null }
console.log(partial, full);Why `value` Is Partial
FormGroup.value leaves out disabled controls. Any key could therefore be missing, so Angular types value as Partial<...>. When you need every field with no undefined, call getRawValue(). A later chapter covers disabled controls in detail.
| Expression | Type | Column | Column |
|---|---|---|---|
| `new FormControl('')` | `FormControl<string \ | null>` | |
| `new FormControl('', { nonNullable: true })` | `FormControl<string>` | ||
| `new FormControl<number \ | null>(null)` | `FormControl<number \ | null>` |
| `group.value` | `Partial<{...}>` | ||
| `group.getRawValue()` | `{...}` (all keys present) |
Common Errors
- Unexpected
nullin values. You forgotnonNullable: true, andreset()cleared the field tonull. new FormControl(null)typed asnull. When the initial value isnull, inference gives the typeFormControl<null>. Add a generic such asFormControl<string | null>(null).- Reading
form.value.nameasstring. Its type isstring | undefined. UsegetRawValue()or handle theundefinedcase.
Exercise
You declare const city = new FormControl('Paris');, call city.reset(), and pass city.value to a function that takes string. Why does TypeScript reject this, and what one-line change fixes it so that reset returns 'Paris'?
Show answer
Without nonNullable, the control's type is FormControl<string | null>, and reset() sets the value to null, so city.value has type string | null. Declare it as new FormControl('Paris', { nonNullable: true }). The type becomes FormControl<string>, and reset() restores 'Paris'.