Dev Helpers
← Back

Clear a field error immutably

Clear a field error immutably using TypeScript.

Open this example in the interactive library

Source code

type Fields = { name: string; age: number; subscribed: boolean };
const initial: Fields = { name: "Ada", age: 30, subscribed: false };
console.log(
  ((errors: Partial<Record<keyof Fields, string>>) => {
    const { age, ...rest } = errors;
    return rest;
  })({ name: "Required", age: "Invalid" }),
);

Sample output

{ name: 'Required' }