← Back to Angular, page 8

angular-0148 · Hard senior

How do typed reactive forms work, and how would you write a small helper that builds a typed FormGroup and extracts the typed value?

Typed forms infer control types from FormControl and FormGroup. new FormControl('') is string or null because reset sets null, so use nonNullable to get string. value is Partial because disabled controls are left out, and getRawValue gives the full type. A generic helper can build a form from a model with types preserved.

Full answer

Since Angular 14, FormControl<T>, FormGroup<{...}> and FormArray<T> carry types.

Key rules:

  1. new FormControl('') has type FormControl<string | null>, because reset() sets the value to null. Use new FormControl('', { nonNullable: true }) so reset goes back to the initial value, and the type is string. FormBuilder.nonNullable.group({...}) applies this to all controls.
  2. group.value has type Partial<{...}>, because disabled controls are not included. group.getRawValue() includes disabled controls and has the full type.
  3. form.controls.name is typed, and a wrong name is a compile error. form.get('name') is typed for simple paths in newer versions, but controls is clearer.
  4. FormRecord<T> is for dynamic keys with the same type. FormArray<FormControl<string>> for lists.
  5. UntypedFormGroup exists for migration only.

A helper: give a plain model ({ name: string; marks: number }) and get a typed form where each property is a non-nullable control:

type ControlsOf<T> = { [K in keyof T]: FormControl<T[K]> }

function formFor<T extends Record<string, unknown>>(model: T): FormGroup<ControlsOf<T>> creates a FormControl(value, { nonNullable: true }) for each key. This keeps types in one place and avoids repeating them.

Limits: nested groups and arrays need recursive types, which makes types hard to read. For nested forms, build small typed sub forms and compose them. Do not make the type too clever, because error messages become hard to read for the team.

Submit pattern: if (form.invalid) return; const value = form.getRawValue(); gives a fully typed object to send to the server. Map to a DTO type explicitly, so form shape changes do not silently change the API.

typescript
import { FormControl, FormGroup } from '@angular/forms';

type ControlsOf<T> = { [K in keyof T]: FormControl<T[K]> };

export function formFor<T extends Record<string, string | number | boolean>>(model: T): FormGroup<ControlsOf<T>> {
  const controls = {} as ControlsOf<T>;
  for (const key of Object.keys(model) as (keyof T)[]) {
    controls[key] = new FormControl(model[key], { nonNullable: true }) as ControlsOf<T>[keyof T];
  }
  return new FormGroup(controls);
}

interface StudentModel extends Record<string, string | number | boolean> {
  name: string;
  marks: number;
}

const form = formFor<StudentModel>({ name: 'Asha', marks: 72 });
const marks: number = form.controls.marks.value; // number, not number | null
const raw: StudentModel = form.getRawValue(); // full type, includes disabled controls
// form.controls.age;  // compile error: no such control
form.controls.marks.disable();
const partial: Partial<StudentModel> = form.value; // marks is missing at run time
console.log(marks, raw, partial);

Angular 20.3 strict mode, type-checked with `ngc`. The assignments in the example are checked by the compiler. The helper itself was not unit tested.

Key points

  • `FormControl<string | null>` by default. Use `nonNullable` to get `string` and keep the initial value on reset.
  • `value` is Partial because of disabled controls. `getRawValue()` is the full type.
  • A mapped type `ControlsOf<T>` plus a helper builds typed forms from a model.
  • Keep types simple. Map form values to a DTO explicitly.

Common mistakes

  • Forgetting `nonNullable` and getting `null` after reset.
  • Using `value` and expecting all keys.
  • Using `UntypedFormGroup` to avoid type errors.

Follow-ups

  • What is the difference between `FormRecord` and `FormGroup`?
  • How would you type a nested group?
  • Why map the form value to a DTO?

Tips

  • Explain the reset to null reason.
  • Show the mapped type.
  • Say you keep types readable.

Evaluation rubric

  • Explains null handling, Partial value and getRawValue.
  • Writes a correct generic helper.
  • Gives practical typing advice.