escalier-lang / escalier-next

Improved type safety with tight TypeScript interop
https://escalier-lang.github.io/escalier-next/
MIT License
14 stars 0 forks source link

Check if pattern matching works with nested union types #330

Closed kevinbarabash closed 3 months ago

kevinbarabash commented 3 months ago

Can we pattern match on something like:

enum Color {
  RGB {r: number, g: number, b: number},
  HSL {h: number, s: number, l: number},
} 
type Shape = 
  | { kind: "Circle", radius: number, color: Color} 
  | { kind: "Rect", width: number, height: number, color: Color}
kevinbarabash commented 3 months ago

I added the following test case and it seems to work:

        declare let value: {a: [number] | {x: number}} | {b: [number] | {y: number}};
        let result = match value {
          {a: [x]} => x,
          {a: {x}} => x,
          {b: [y]} => y,
          {b: {y}} => y,
        };