Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non generic syntax for generating types with discriminators #677

Open
martitv opened this issue Jan 15, 2024 · 0 comments
Open

Non generic syntax for generating types with discriminators #677

martitv opened this issue Jan 15, 2024 · 0 comments

Comments

@martitv
Copy link

martitv commented Jan 15, 2024

I am generating zod schemas based on types generated by swagger-typescript-api. The schema generation doesn't work well with generics so I was wondering if it would be possible to have an alternative way of generating types with discriminators?

Today, this is the syntax it seems:

export type Value = BaseValue &
  (
    | BaseValueTypeMapping<"Type1", Value1>
    | BaseValueTypeMapping<"Type2", Value2>
    | BaseValueTypeMapping<"Type3", Value3>;

export type Value1 = BaseValue;

export type Value2 = BaseValue ;

export type Value3 = BaseValue ;

interface BaseValue {
  $type: string;
  value: number;
}
    
type BaseValueTypeMapping<Key, Type> = {
  $type: Key;
} & Type;

It would be nice if it was possible to just generate the types with discriminators like so:

export type Value = BaseValue &
  (
    | Value1 
    | Value2 
    | Value3 ;

export type Value1 = BaseValue & {
  $type: "Type1"
};

export type Value2 = BaseValue & {
  $type: "Type2"
};

export type Value3 = BaseValue & {
  $type: "Type3"
};

interface BaseValue {
  $type: string;
  value: number;
}

Is this possible?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant