Skip to content

Instantly share code, notes, and snippets.

@twerske
Created May 13, 2022 17:31
Show Gist options
  • Save twerske/4f9f18f748584abd8edc3e608482922d to your computer and use it in GitHub Desktop.
Save twerske/4f9f18f748584abd8edc3e608482922d to your computer and use it in GitHub Desktop.
Typed Angular Forms
const cat = new FormGroup({
name: new FormGroup({
first: new FormControl('Barb'),
last: new FormControl('Smith'),
}),
lives: new FormControl(9),
});
// Type-checking for forms values!
// TS Error: Property 'substring' does not exist on type 'number'.
let remainingLives = cat.value.lives.substring(1);
// Optional and required controls are enforced!
// TS Error: No overload matches this call.
cat.removeControl('lives');
// FormGroups are aware of their child controls.
// name.middle is never on cat
let catMiddleName = cat.get('name.middle');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment