Dev Helpers
← Back

Build a typed field descriptor union

Build a typed field descriptor union 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(
  (() => {
    type Field = {
      [K in keyof Fields]: { name: K; value: Fields[K] };
    }[keyof Fields];
    const f: Field = { name: "age", value: 30 };
    return f;
  })(),
);

Sample output

{ name: 'age', value: 30 }