0

Let's say these are the basic types I'm working with:

type MyUnion = 'a' | 'b' | 'c'
type MyType = {myKey: MyUnion}

Hovering over MyType doesn't expand MyUnion:

enter image description here

So I created a helper type that recursively expands deeply nested types:

type Expand<T> = {
  [K in keyof T]: T[K]
} & {}

Let me show you Expand in action. Here's some types I'll play around with:

type SomeObj = {
  a: number,
  b: string
}
type TopLevel = {c: SomeObj}
type TopLevelExpanded = Expand<TopLevel>

Hovering over TopLevel shows the "before":

enter image description here

And hovering over TopLevelExpanded shows the "after", with the nested types expanded:

enter image description here

Putting it all together, I had hoped that using this Expand type helper would also expand unions, but it doesn't:

enter image description here

Is there a way in TypeScript to expand union types in Intellisense when hovering? I can't figure out what's wrong with my Expand helper type.

The Qodesmith
  • 3,205
  • 4
  • 32
  • 45
  • A union type isn’t an object so your `Expand` utility function wouldn’t work. – Terry Mar 19 '23 at 14:18
  • Does this answer your question? [How to get helpful intellisense when using a type alias for a string union type as a property of an object or interface?](https://stackoverflow.com/questions/73588194/how-to-get-helpful-intellisense-when-using-a-type-alias-for-a-string-union-type) – kelsny Mar 19 '23 at 14:55
  • [Here's an example](https://tsplay.dev/WKpoDw) of a technique I've used for expanding unions. If that's satisfactory as an answer to your question, I can write it up as one. If not, can you update the question to explain what it doesn't address? – jsejcksn Mar 19 '23 at 19:47
  • @vr. That's the same question I'm asking! And that solution seems to work if you hover over individual destructured arguments, not the object type itself. I'm trying to see the union expanded why I hover over the object type itself. – The Qodesmith Mar 20 '23 at 10:52
  • @jsejcksn As soon as I [put the type in an object](https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&removeComments=true&target=99&jsx=4&useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true&inlineSourceMap=true&inlineSources=true#code/C4TwDgpgBAqgjFAvFA1hEB7AZlAEgFQFkAZAUQBsIBbCAO2HwEMBzAOUZsMbAG4AoKIID0QgHoB+PiKiBeDcAke3z4BjDLQDOwKAFdaASwCOWiAGUQVJFFNUARhnIAKAJT9QkWACYLpAB5LyWgBMIAB40TBwCEgpqOgYWdk5uABooVwhsbT1DEzMAPn5BKBEJKSEoQD4NwEpdxTSoAHlrACsLAG8BYTFJQsYALg8+AF8+IA), the expanded union goes away :( – The Qodesmith Mar 20 '23 at 10:56

0 Answers0