ts-interface-builder icon indicating copy to clipboard operation
ts-interface-builder copied to clipboard

Any plans to implement generics or typescript utilities?

Open dwjohnston opened this issue 5 years ago • 4 comments

I'm wanting to do something like this:

interface Foo {
    id: string; 
    biz: string; 
    bar: string; 
}

type PartialFoo = Pick<Foo, "id" | "biz">; 

Pretty simple.

But the compiler will give me:

Error: Generics are not yet supported by ts-interface-builder:

Is there any plan to incorporate this support?

dwjohnston avatar Nov 14 '19 03:11 dwjohnston

Hmmm, looking at the code, it looks like it might be easy enough to support some of the basic utility types. If I get some time, I'll see if I can do it.

https://github.com/gristlabs/ts-interface-builder/blob/9ac5de2f2b2e9cf556372c54df74913291036c28/lib/index.ts#L132

dwjohnston avatar Nov 14 '19 04:11 dwjohnston

Implementing specific generics like Pick seems reasonably straightforward. Implementing generics generally (or even just mapped types generally) is probably doable too, but requires a much deeper understanding of typescript internals.

dsagal avatar Nov 14 '19 06:11 dsagal

I'd like to try and implement support for few generic types that I am using i.e. Pick Omit Partial NonNullable and so on. Could you briefly explain where / how this should be done so I can give it a go?

xzilja avatar Mar 25 '21 15:03 xzilja

There are two generic types handled: Array<T> and Promise<T> (the latter is just treated as T, which happens to be helpful for some cases). See _compileTypeReferenceNode method. That's where the processing would start, I imagine.

As for how to process it, it would be nice to do something that could extend to generics more generally later. E.g. translate Pick<T> maybe to t.instance('Pick', 'T'), and add support on ts-interface-checker side, which recognizes a few names of generics, looks up the type object for the type argument(s), and does something custom to turn those type objects into new ones.

dsagal avatar Mar 25 '21 16:03 dsagal