grantila / typeconv

Convert between JSON Schema, TypeScript, GraphQL, Open API and SureType
MIT License
424 stars 8 forks source link

Convert interfaces/types that implements generic types #36

Open juansie96 opened 1 year ago

juansie96 commented 1 year ago

Hello! I expect this to work:

export type FinalType = GenericType<{ foo: number; bar: string }>
export type GenericType<T> = T

but it isn't recognizing type params, all my types with generic type implementation are not constructed on my .swagger.yaml oapi output file. What can I do or it's not supported?

RafaelRamosR commented 1 year ago

I have the same problem, it seems to ignore generics.

import { getTypeScriptReader, getOpenApiWriter, makeConverter } from 'typeconv';

const input = 'export interface Foo<T> { data: T[]; success: boolean; }';

const reader = getTypeScriptReader();
const writer = getOpenApiWriter({ format: 'yaml' });
const { convert } = makeConverter(reader, writer);
const { data } = await convert({ data: input });

console.log(data);

Output:

openapi: 3.0.0
info:
  title: Converted with typeconv
  version: '1'
  x-comment: >-
    Generated by core-types-json-schema
    (https://github.com/grantila/core-types-json-schema) on behalf of typeconv
    (https://github.com/grantila/typeconv)
paths: {}
components:
  schemas: {}

As a curiosity, when the generic is omitted, this result is obtained:

...
const input = 'export interface Foo<> { data: T[]; success: boolean; }';
...

Output:

openapi: 3.0.0
info:
  title: Converted with typeconv
  version: '1'
  x-comment: >-
    Generated by core-types-json-schema
    (https://github.com/grantila/core-types-json-schema) on behalf of typeconv
    (https://github.com/grantila/typeconv)
paths: {}
components:
  schemas:
    Foo:
      properties:
        data:
          items: {}
          title: Foo.data
          type: array
        success:
          title: Foo.success
          type: boolean
      required:
        - data
        - success
      additionalProperties: false
      title: Foo
      type: object
microsoftly commented 6 months ago

+1 here. Definitely reduces the utility of this without having generics be converted.

Jerome1337 commented 1 month ago

This should be great! Sometimes generics are almost mandatory