runtyping icon indicating copy to clipboard operation
runtyping copied to clipboard

Exporting static types even when "exportStaticType" is false

Open sstur opened this issue 3 years ago • 3 comments

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
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
import { Record, Literal, String, Static, Number } from 'runtypes';

export const One = Record({ type: Literal("one"), value: String, });

export type One = Static<typeof One>;

export const Two = Record({ type: Literal("two"), value: Number, });

export type Two = Static<typeof Two>;

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!

sstur avatar Jul 08 '22 02:07 sstur

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.

johngeorgewright avatar Jul 11 '22 08:07 johngeorgewright

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.

sstur avatar Jul 12 '22 02:07 sstur

Problems with excluding static type exports fixed in 4.6.0.

johngeorgewright avatar Jul 12 '22 18:07 johngeorgewright