Typescript: Get interface that consist of properties that are present in interface A, but not in interface B

Viewed 36

I'm writing a mapper to translate between a serialized entity state and a form state.

Given two interfaces A and B: How can I get a third interface C that contains the properties that are present in A but missing in B?

interface A {
    name: string;
    age: number;
    id: string;
    version: number;
}

// could be written interface B extends Omit<A, "version" | "id"> {}
interface B {
    name: string;
    age: number;
}

interface PropertiesThatArePresentInAButNotInB {
    // ???
}

// this is my mapper interface (just for illustrating the use case)
interface IEntityFormStateMapper<
  E extends Entity<A>,
  FS extends B,
  OP extends PropertiesThatArePresentInAButNotInB
> {
  toEntity: (formState: FS, omittedEntityProps: OP) => Result<Error, E>;
  toFormState: (entity?: E) => Result<Error, [FS, OP]>;
}
2 Answers

What you want to do is as simple as Omit<A, keyof B>.

You can give this an alias like type PropertiesOfAButNotB = Omit<A, keyof B> if you are going to use it in multiple places.

Related