#Type that returns the shape of T, and makes props required if they are required in U?

12 messages · Page 1 of 1 (latest)

stoic estuary
#

What I'm hoping for:

type A = {
  a: string;
  b?: string;
};

type B = {
  b: string;
};

const x: Type<A, B> = {
  a: "hello",
}; // error, b is missing
#

Thanks in advance

worthy forge
#

A & B

#

it's that simple

stoic estuary
#

Wow

#

lol. What would I do to exclude props in B that aren't in A/

worthy forge
#

hum, I guess it works for the simple cases

worthy forge
hidden plinthBOT
#
type A = {
    a: string;
    b?: string;
};

type B = {
    b: string;
    c: number;
};

type Type<T, U> = T & Pick<U, keyof (T | U)>;

const x: Type<A, B> = {
//    ^
// Type '{ a: string; }' is not assignable to type 'Type<A, B>'.
//   Property 'b' is missing in type '{ a: string; }' but required in type 'Pick<B, "b">'.
//    ^? - const x: Type<A, B>
    a: "hello",
};
worthy forge
#

@stoic estuary

stoic estuary
#

That's awesome. Thank you!

#

Going to give this a shot.