typeconv icon indicating copy to clipboard operation
typeconv copied to clipboard

Convert interfaces/types that implements generic types

Open juansie96 opened this issue 1 year ago • 3 comments

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?

juansie96 avatar Jun 07 '23 00:06 juansie96

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

RafaelRamosR avatar Jun 07 '23 16:06 RafaelRamosR

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

microsoftly avatar May 22 '24 19:05 microsoftly