Sobes.tech
Middle+

How to declare a new type based on Foo, but without fields a and c, without using Omit?

sobes.tech AI

Answer from AI

To declare a new type based on Foo, excluding fields a and c without using Omit, you can use intersection with Pick and Exclude. For example, if Foo has fields a, b, c, d, you can explicitly specify the desired fields:

interface Foo {
  a: string;
  b: number;
  c: boolean;
  d: Date;
}

type WithoutAandC = Pick<Foo, Exclude<keyof Foo, 'a' | 'c'>>;

Here, Exclude<keyof Foo, 'a' | 'c'> excludes the keys a and c from all keys of Foo, and Pick selects the remaining fields. Thus, you get a type without fields a and c without directly using Omit.