Angular 14 ships with typed reactive forms, and it's just great. Type inference, backward compatibility, fine-tuning options, and user-defined type definitions - it's all there. It's been a long wait, but it was worth it!
UntypedFormControl
instead of FormControl
.
string | null
puzzled me at first. You can get rid of it by adding an option:
[sourcecode TypeScript]
const name = new FormControl('', { nonNullable: true }); // --> string
[/sourcecode]
This guarantees the form control is never null. The null value is confusing because form controls reflect the user input, and users never can input null values. Even if they don't input anything, it's still an empty string. However, as Netanal Basal explains, FormControl.reset()
sets the value to null unless you add the nonNullable
attribute.
FormControl
has become a generic type. So you can override the default by passing a type parameter:
[sourcecode TypeScript]
const salary = new FormControlFormGroups
, as Netanal Basal reports::
[sourcecode TypeScript]
// please read Netanals blog for an
// explanation of this type magic :)
export type ControlsOf