How to get the values from a generic type object?

45 views Asked by At

I am trying to create a composite generic type created from the first argument object.

const x = {
  one: (x: {a: boolean; b: boolean}) => true,
  two: (x: {c: boolean; d: boolean}) => true,
};

type VVVV<t> = {
  [k in keyof t]: t[k] extends (...args: any) => any ? Parameters<t[k]>[0] : {};
};

type l = VVVV<typeof x>;

type currentOutput = {
  one: {
    a: boolean;
    b: boolean;
  };
  two: {
    c: boolean;
    d: boolean;
  };
};

type desiredOutput = {
  a: boolean;
  b: boolean;
  c: boolean;
  d: boolean;
};

How do I create a type that looks like desiredOutput from the current output of VVVV which looks like currentOutput?

1

There are 1 answers

0
ThomasReggi On

I was looking for this :(

type VVVV<t> = {
  [k in keyof t]: t[k] extends (...args: any) => any ? Parameters<t[k]>[0] : {};
}[keyof t];