johngeorgewright / runtyping

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

Exporting static types even when "exportStaticType" is false #313

Open sstur opened 2 years ago

sstur commented 2 years ago

This doesn't happen in all cases, but it seems to be the case with a tagged union. I have a one-click repro for you to check out here: https://stackblitz.com/edit/node-5bqrlm?file=src/main.ts

The source types in that example looks like this:

Click to expand ```ts type One = { type: 'one'; value: string; }; type Two = { type: 'two'; value: number; }; export type Three = One | Two; ```

You'll see the generated output is at "src/generated/runtypes.ts" which was generated by running ts-node src/main.ts and it looks like this:

Click to expand ```ts import { Record, Literal, String, Static, Number } from 'runtypes'; export const One = Record({ type: Literal("one"), value: String, }); export type One = Static; export const Two = Record({ type: Literal("two"), value: Number, }); export type Two = Static; export const Three = One.Or(Two); ```

Notice in main.ts I only asked for the type Three (which is the only exported member in types.ts) and I also specified exportStaticType: false, yet I got 6 exports in the generated output, 3 of which are types.

Any idea why this would be? If you can confirm this is a bug and if you could point me in the right direction I might be able to get a PR together.

Thanks!

johngeorgewright commented 2 years ago

Runtypes One & Two have been generated as they are dependents of Three and all runtypes are exported by default. If you can think of a scenario in which exporting the dependents is a problem we can look in to changing that.

The fact that the static types are being generated, even when configured not to do so, looks like a bug.

sstur commented 2 years ago

all runtypes are exported by default. If you can think of a scenario in which exporting the dependents is a problem we can look in to changing that

OK, makes sense, I didn't realize that. In most cases this isn't an issue, the only very minor thing is that when editing other files VS Code will populate auto-complete suggestions based on exports of files in the same project, so it creates a bit more noise in that situation.

The fact that the static types are being generated, even when configured not to do so, looks like a bug.

Got it, happy to help come up with a solution if I can.

johngeorgewright commented 2 years ago

Problems with excluding static type exports fixed in 4.6.0.