I\'m looking to refactor a large set of components in my Angular project to have strongly typed FormGroups, FormArrays, and FormControls.
I\'m just looking for a good wa
The most elegant solution is leveraging TypeScript declaration files (*.d.ts
) to introduce generic interfaces extending the standard form classes like AbstractControl
, FormControl
, etc. It doesn’t introduce any new functionality and has no footprint in the compiled JavaScript, but at the same time enforcing strong type checking.
It was suggested by Daniele Morosinotto in March this year and there are talks now to include it in Angular 9.
Adopting the solution is straightforward:
TypedForms.d.ts
from this gist and save it as src/typings.d.ts
in your project (Angular 6+ already knows how to use this file).FormGroupTyped
, FormControlTyped
, etc.) whenever you need a strong type validation (see examples in that gist or stackblitz).For more information, check out a blog post analysing available solutions for strongly typed forms.