#How to Omit all Keys from one type based on a second type?

3 messages · Page 1 of 1 (latest)

floral summit
#

Given these interfaces

interface TypeA {
  data: { a: string; };
  value: string;
  age: number;
}
interface TypeB {
  data: { b: number; };
  value: number;
  isGood: boolean;
};

How can I automatically generate this type?
All props in TypeB replace the props in TypeA

Combined {
  data: { b: number; }
  value: number;
  age: number;
  isGood: boolean;
}

I can do it manually with this:

type Combined1 = Omit<TypeA, 'data' | 'value'> & TypeB;

But I'm unsure how to do it automatically?

I've noticed that I can Omit all of the keys from TypeB from TypeA without an error.
ie: isGood isn't present in TypeA, but omitting it doesn't cause an issue. Is that helpful?

type Combined1 = Omit<TypeA, 'data' | 'value' | 'isGood'> & TypeB;
#

🦆 Rubber duck!

#

This works:

type Combined1 = Omit<TypeA, keyof TypeB> & TypeB;