1

In Typescript, when inspecting types defined by intersection, I see a hint identical to original definition:

enter image description here

but I would like to see the resulting shape, something like this:

enter image description here

what is a quick / efficient way to do this?

Ivan Koshelev
  • 3,830
  • 2
  • 30
  • 50
  • Please edit the question to include the code in the images as text also, as described in [ask]. – jcalz May 01 '20 at 14:39
  • Possible duplicate of [How can I see the full expanded contract of a Typescript type?](https://stackoverflow.com/questions/57683303/how-can-i-see-the-full-expanded-contract-of-a-typescript-type) – jcalz May 01 '20 at 14:46

1 Answers1

1

I don't think there's any way to ask your IDE to do this automatically, but you can make a type function that calculates the type you're looking for. From the answer to the question is duplicates, I would write Expand:

type Expand<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;

which takes a type (or union of types) and walks through all of its properties, even if they come from multiple intersections, and packages it (or each union member) into a single object type. Then you could either write Expand<C> or you could define C to be Expand<A & B>:

type ExpandedC = Expand<A & B>;
/*
type ExpandedC = {
    a: number;
    b: string;
    c: boolean;
    d: Date;
}
*/

which is what you wanted. Okay, hope that helps; good luck!

Playground link to code

jcalz
  • 264,269
  • 27
  • 359
  • 360