3

I have a repo with a lot of complex types for the project I'm working on. Interfaces that extend other interfaces, mapped types, generic types etc etc etc.

For example let's say I have

interface Base {
  foo: string;
  bar: string;
}

interface Something<T extends string> extends Base {
  baz: T;
}

interface A extends Something<'hello'> {
  qux: string;
}
interface B extends Something<'world'> {
  abc: number
}
interface C extends Something<'goodbye'> {
  def: boolean;
}

interface Woop<T extends 'a' | 'b' | 'c'> {
  a: A;
  b: B;
  c: C;
}[T];

...and so on, just to get an idea.

Now, for documentation purposes, I need to send some of these types to a co-worker who doesn't work with TypeScript, so that he can easily see what properties the interface C has. It doesn't seem very useful to send him a large complex TypeScript file with lots of extended interfaces, generics, mapped types etc, so I'm looking for a way (tool?) to break down a set of types into primitives, so that I can send him this instead:

interface Woop = {
  foo: string;
  bar: string;
  baz: 'hello'
  qux: string;
} | {
  foo: string;
  bar: string;
  baz: 'world'
  abc: number;
} | {
  foo: string;
  bar: string;
  baz: 'goodbye'
  def: boolean;
}

Is there such a tool? It feels similar to what IDEs do when inspecting types during development, it usually simplifies/flattens complex types into their combined properties etc.

JHH
  • 8,567
  • 8
  • 47
  • 91
  • ① It would be nice if the code you provided were a [mre] instead of pseudocode, so that there would be a way to test if we did it right. Could you try it out in an IDE and fix any problem unrelated to what you're asking about? ② By "tool" I assume you're looking for some different build step as opposed to a technique for getting this info in an IDE (since when you inspect types in an IDE it often does *not* simplify things the way you're talking about, and you need to [do things](https://stackoverflow.com/q/57683303/2887218) to get that if you want it), right? Or would an IDE solution work? – jcalz Jul 04 '23 at 13:59

0 Answers0