0

I try the following

    new FormControl<boolean | undefined>({ value: true }, Validators.required),

and get the error that no overloads for boolean exists. Using

    new FormControl<string | null>({ value: null, disabled: false }));

works ==> So, what is the correct syntax in Angular 14 for Typed FormControls for boolean?

Amer
  • 6,162
  • 2
  • 8
  • 34
LeO
  • 4,238
  • 4
  • 48
  • 88

1 Answers1

3

You can define it like the following:

new FormControl<boolean>(true, Validators.required);

And you can pass options to it like the following:

new FormControl<boolean>(true, {
  updateOn: 'submit',
  nonNullable: true,
  validators: Validators.required,
});

Read more about Angular 14 Typed Forms here: https://angular.io/guide/typed-forms#specifying-an-explicit-type

Amer
  • 6,162
  • 2
  • 8
  • 34
  • Just a q: this means that the option for value is not required, that the default value is set as the first argument of the constructor. So far so good. Where to find the other options documented? E g nonNullable is mentioned in the link but not within a list. Or what does the updateOn means, etc. – LeO Aug 20 '22 at 18:24
  • Please check the following links: https://angular.io/api/forms/FormControl#constructor & https://angular.io/api/forms/FormControlOptions & https://angular.io/api/forms/AbstractControlOptions & https://indepth.dev/posts/1311/the-update-on-option-in-angular-forms – Amer Aug 21 '22 at 06:50