typoa icon indicating copy to clipboard operation
typoa copied to clipboard

Polymorph interfaces

Open derolf opened this issue 4 years ago • 4 comments

Consider

interface Foo {
  foo: number;
}
interface Bar extends Foo {
  bar: number;
}

Currently, Bar generates a "flat" representation. However, we should embed Foo using allOf into Bar.

Happy to work on a PR if you agree!

derolf avatar Sep 09 '21 14:09 derolf

Hello,

Yes, you can try to open a PR if you want

Eywek avatar Sep 09 '21 14:09 Eywek

Working on it already, do you know how to use ts-morph to only get the own props of a node?

BTW. I think that ONLY interface should generate explicit schema objects, all other types should always be inlined.

Otherwise, you get a bunch of weird intermediate explicit schema types in the schema, like this:

        Patched__title-string--__:
            type: object
            properties:
                title:
                    type: string

which stems from Patched<{title: string}> with

export type Patched<T> = {
  [P in keyof T]?: T[P] extends Array<infer U> ? Array<Patched<U>> : T[P] extends ReadonlyArray<infer U> ? ReadonlyArray<Patched<U>> : Patched<T[P]>;
};

These make it hard to maintain a stable OpenAPI spec and the codegens and users downstream cry. (BTW I have handwritten codegens for openAPI -> TypeScript and openAPI -> Dart)

derolf avatar Sep 09 '21 17:09 derolf

do you know how to use ts-morph to only get the own props of a node?

Nope sorry, I think you'll need to dig into ts-morph to find the method

I think that ONLY interface should generate explicit schema objects, all other types should always be inlined

IMO interfaces & types (i.e. type Foo = { bar: string } or interface Foo { bar: string }) (not only interfaces since not everyone use interfaces) should generate explicit schema objects and mapped types should be always inlined

Eywek avatar Sep 10 '21 08:09 Eywek

Otherwise, you get a bunch of weird intermediate explicit schema types in the schema, like this:

should be better with https://github.com/Eywek/typoa/commit/bba5a2445d899cc310bd19a8558bad38f976fdb5

Eywek avatar Mar 28 '22 08:03 Eywek