fable-compiler / ts2fable

Parser of Typescript declaration files
http://fable.io/ts2fable/
Apache License 2.0
224 stars 34 forks source link

Replace Unions of Enum-Sub-Sets with source Enum #451

Closed Booksbaum closed 1 year ago

Booksbaum commented 1 year ago
enum RootKind {
  Alpha = 1,
  Beta = 2,
  Gamma = 3,
  Delta = 4,
}

type SubSet1 = RootKind.Alpha | RootKind.Beta
type SubSet2 = RootKind.Beta | RootKind.Gamma

type SubSubSet = SubSet1 | SubSet2

->

type [<RequireQualifiedAccess>] RootKind =
    | Alpha = 1
    | Beta = 2
    | Gamma = 3
    | Delta = 4

type SubSet1 =
    RootKind

type SubSet2 =
    RootKind

type UnionSubSet =
    U2<SubSet1, SubSet2>

-> SubSet1 & SubSet2 are replaced with their source Enum -- but UnionSubSet stays U2 because it doesn't use Enum Cases directly, but via other SubSets of source Enum.

With this PR Unions of Sub-Sets get reduced too:

type UnionSubSet = RootKind



Additional: I now place a remarks XML comment with the TypeScript Definition on the Alias:

/// <remarks>
/// Original in TypeScript:  
/// <code lang="typescript">
/// RootKind.Alpha | RootKind.Gamma
/// </code>
/// </remarks>
type SubSet1 =
    RootKind

/// <remarks>
/// Original in TypeScript:  
/// <code lang="typescript">
/// Sub1Kind | Sub1Kind
/// </code>
/// </remarks>
type UnionSubSet =
    RootKind