johngeorgewright / runtyping

Generate runtype validation from static types & JSON schema.
MIT License
46 stars 5 forks source link

Generics are not generated properly #407

Open AsPulse opened 2 years ago

AsPulse commented 2 years ago

Generated:

Record({ abc: <{ data: string; } extends any, > ({ data: string; }: Runtype<{ data: string; }>,) => Record({ data: String, }).And(Record({ count: Number, })),});

I don't think the bottom part is necessary and gills in my opinion. Is it necessary for some generative process...? <{ data: string; } extends any, > ({ data: string; }: Runtype<{ data: string; }>,) =>

johngeorgewright commented 2 years ago

Hey @AsPulse. I just tried your example in #398, but I get a different result to what you're saying here. https://stackblitz.com/edit/node-atzfxo?file=src/index.runtypes.ts Can you provide me with a full example?

AsPulse commented 2 years ago

Sorry, I checked and it seems that the problem only occurs when importing a Type containing Generics from another file (external.ts in the example below).

https://stackblitz.com/edit/node-ahtzsa?file=src/external.ts

johngeorgewright commented 2 years ago

So, finally got around to having a look at this. The problem boils down to a bug when importing types from other files. It's a toughy, and I'll get around to it soon. In the mean time, if at all possible, the work around is to specify the source file of the type you're trying to build.

Instead of:

- targetFile: ./src/index.runtypes.ts
  sourceTypes:
    - file: ./src/index.ts
      type: [Foo]

Use:

- targetFile: ./src/index.runtypes.ts
  sourceTypes:
    - file: ./src/external.ts
      type: [Bar]