@@ -539,20 +539,35 @@ module Type =
539
539
boundTyprms |> List.map ( fun x -> TypeVar x.name),
540
540
MultipleLocation ( funcs |> List.map ( fun f -> f.loc))
541
541
)
542
+ let normalizeUnion ( u : UnionType ) : UnionType =
543
+ let rec go ts =
544
+ ts |> List.collect ( function
545
+ | Union u -> go u.types
546
+ | t -> [ t]
547
+ )
548
+ { u with types = go u.types |> List.distinct }
549
+
550
+ let normalizeIntersection ( i : IntersectionType ) : IntersectionType =
551
+ let rec go ts =
552
+ ts |> List.collect ( function
553
+ | Intersection i -> go i.types
554
+ | t -> [ t]
555
+ )
556
+ { i with types = go i.types |> List.distinct }
542
557
543
558
// TODO: more optimization
544
- let createUnion ( _ctx : TyperContext < _ , _ >) ( types : Type list ) =
559
+ let createUnion ( types : Type list ) =
545
560
match types with
546
561
| [] -> Prim Never
547
562
| [ x] -> x
548
- | _ -> Union { types = types }
563
+ | _ -> Union ( normalizeUnion { types = types })
549
564
550
565
// TODO: more optimization
551
- let createIntersection ( _ctx : TyperContext < _ , _ >) ( types : Type list ) =
566
+ let createIntersection ( types : Type list ) =
552
567
match types with
553
568
| [] -> Prim Any
554
569
| [ x] -> x
555
- | _ -> Intersection { types = types }
570
+ | _ -> Intersection ( normalizeIntersection { types = types })
556
571
557
572
let substTypeVarInInheritingType subst ctx = function
558
573
| InheritingType.KnownIdent x ->
@@ -694,7 +709,7 @@ module Type =
694
709
let members = c.members |> List.map snd
695
710
let intersection = function
696
711
| [] -> None
697
- | ts -> createIntersection ctx ts |> Some
712
+ | ts -> createIntersection ts |> Some
698
713
let rec go = function
699
714
| TypeLiteral ( LString name) ->
700
715
let funcs , others =
@@ -815,7 +830,7 @@ module Type =
815
830
| _ -> None)
816
831
match types with
817
832
| [] -> onFail ()
818
- | _ -> createIntersection ctx types
833
+ | _ -> createIntersection types
819
834
820
835
| Keyof t ->
821
836
let t = resolveErasedTypeImpl typeQueries ctx t
0 commit comments