Effect CommunityEC
Effect Community3y ago
31 replies
Wix

Issue with Creating Union Types in TypeScript

Here's an odd question. I often create my types like this
export interface CancelledChildRecurrenceEvent
  extends S.Schema.To<typeof CancelledChildRecurrenceEventWithDiscriminant> {}
So, I get the interface name instead of type structure in IDEs like VSCode. The problem is S.union is obviously unaware of the interface and only uses the type. I find myself creating union types that are a union of the interfaces above rather than using S.Schema.To<typeof MyUnion> but then I frequently have to map my effects with .map(e => e as MyUnion) so it uses the handrolled union type. Here is a small example of what I mean:
export interface MyTypeA
  extends S.Schema.To<typeof MyTypeA> {}
export interface MyTypeB
  extends S.Schema.To<typeof MyTypeB> {}

export const MyType = S.union(
  MyTypeA,
  MyTypeB
);

export type MyType = MyTypeA | MyTypeB

S.parseSync(MyType)(bleh).pipe(Effect.map(e => e as MyType))


Does anyone know a more elegant way to do this? Either some VSCode switch to use type names instead of their structure or an easy way for union to use my other interface? Thanks!
Was this page helpful?