jasonkuhrt / alge

Type safe library for creating Algebraic Data Types (ADTs) in TypeScript. 🌱

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shorthand API

jasonkuhrt opened this issue · comments

Perceived Problem

@Weakky pointed out that for simple cases the current API is verbose. I agree!

Ideas / Proposed Solution(s)

I sketched out some alternatives here: https://gist.github.com/jasonkuhrt/7ad238bcc13e853ff7456f624f794879

I think this could work:

//--------------------------------------------------------------------------------------
// Centralized

const Shape = Alge.union(`Shape`, {
  Rectangle: {
    width: Length,
    height: Length,
  },
  Circle: {
    radius: Length,
  },
  Square: {
    size: Length,
  },
})

//--------------------------------------------------------------------------------------
// Separated

const Rectangle = Alge.record(`Rectangle`, {
  width: Length,
  height: Length,
})

const Circle = Alge.record(`Circle`, {
  radius: Length,
})

const Square = Alge.record(`Square`, {
  size: Length,
})

const Shape = Alge.union(`Shape`, {
  Rectangle,
  Circle,
  Square,
})