Home > Back-end >  How to select common parameters from two interfaces in Typescript?
How to select common parameters from two interfaces in Typescript?

Time:12-20

Select must be not only by key name but by types of these params

For example:

    interface human {
      name: string,
      height: number,
      weight:number
    }

    interface robot {
      name: number,
      height: number,
      weight:number
    }

Result must be:

    interface commonHumanRobot{
      height: number,
      weight: number
    }

There is a solution based on the answer of @Acid Coder

type selectCommon<T extends object, B extends object> = {
  [key in keyof T as T[key] extends B[key&keyof B]? B[key&keyof B] extends T[key]? key:never:never]:T[key]
}

CodePudding user response:

interface human {
    name: string,
    height: number,
    weight: number
}

interface robot {
    name: number,
    height: number,
    weight: number
}

type SelectCommon<T, V> =
    { [K in keyof T]: T[K] extends V[K & keyof V] ? V[K & keyof V] extends T[K] ? T[K] : never : never }

type RemoveNeverType<T> = { [K in keyof T as T[K] extends never ? never : K]: T[K] }

type A = RemoveNeverType<SelectCommon<human, robot>>
//   ^?

enter image description here

enter image description here playground

however this come with less safe type eg 1 & number is 1, in my longer example, this will result in never which I think is safer but I guess 1 & number is 1 does not have any risk, so it makes sense too

CodePudding user response:

type NonNeverKeys<Type1, Type2, Inter = Type1 & Type2> = {
    [Key in keyof Inter]: Inter[Key] extends never ? never : Key
}[keyof Inter];
type NonNeverIntersect<Type1, Type2> = Pick<Type1 & Type2, NonNeverKeys<Type1, Type2>>;

interface human {
  name: string,
  height: number,
  weight: number
}

interface robot {
  name: number,
  height: number,
  weight: number
}

const inter: NonNeverIntersect<human, robot>;
  • Related