Effect CommunityEC
Effect Community3w ago
3 replies
Alliana

Defining a Discriminated Union with Schema.Class in TypeScript

I am trying to define a structure with states of pending, approved, or rejected using Schema.Class. I defined a Union of such Schema.Class, but it doesn't seem to be a Discriminated Union. How should I define such structures in this case?

import { Schema as S } from 'effect'

export class Pending extends S.TaggedClass<Pending>("Pending")("Pending", {}){
  approve(): Approved {
    return new Approved({})
  }
  
  reject(reason: string): Rejected {
    return new Rejected({reason})
  }
}

export class Approved extends S.TaggedClass<Approved>("Approved")("Approved", {}){}

export class Rejected extends S.TaggedClass<Rejected>("Rejected")("Rejected", {
  reason: S.String
}){}

export const LicenseState = S.Union(Pending, Approved, Rejected)
export type LicenseState = typeof LicenseState.Type
//          ^? = type LicenseState = never
Was this page helpful?