glutinum-org / cli

https://glutinum.net/
59 stars 6 forks source link

unhandled object union type simulating discriminated union #147

Open joprice opened 2 weeks ago

joprice commented 2 weeks ago

Issue created from Glutinum Tool

Glutinum version - 0.11.0-preview

TypeScript

  type SplitAtomAction<Item> = {
      type: 'remove';
      atom: PrimitiveAtom<Item>;
  } | {
      type: 'insert';
      value: Item;
      before?: PrimitiveAtom<Item>;
  } | {
      type: 'move';
      atom: PrimitiveAtom<Item>;
      before?: PrimitiveAtom<Item>;
  };

FSharp

module rec Glutinum

open Fable.Core
open Fable.Core.JsInterop
open System

[<AllowNullLiteral>]
[<Interface>]
type SplitAtomAction =
    abstract member ``type``: string with get, set
    abstract member atom: PrimitiveAtom<'Item> with get, set
    abstract member ``type``: string with get, set
    abstract member value: 'Item with get, set
    abstract member before: PrimitiveAtom<'Item> option with get, set
    abstract member ``type``: string with get, set
    abstract member atom: PrimitiveAtom<'Item> with get, set
    abstract member before: PrimitiveAtom<'Item> option with get, set

Problem description

The pattern in typescript of using a union of object types that share a singleton typed discriminator field ends up generating an fsharp type with the union of all fields, including duplicates. This could either be detected by finding the intersection of fields in the union, then checking if they are the same type and if they are singleton types (string / maybe int as well?), and then generating a DU over them.