@@ -539,20 +539,35 @@ module Type =
539539 boundTyprms |> List.map ( fun x -> TypeVar x.name),
540540 MultipleLocation ( funcs |> List.map ( fun f -> f.loc))
541541 )
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 }
542557
543558 // TODO: more optimization
544- let createUnion ( _ctx : TyperContext < _ , _ >) ( types : Type list ) =
559+ let createUnion ( types : Type list ) =
545560 match types with
546561 | [] -> Prim Never
547562 | [ x] -> x
548- | _ -> Union { types = types }
563+ | _ -> Union ( normalizeUnion { types = types })
549564
550565 // TODO: more optimization
551- let createIntersection ( _ctx : TyperContext < _ , _ >) ( types : Type list ) =
566+ let createIntersection ( types : Type list ) =
552567 match types with
553568 | [] -> Prim Any
554569 | [ x] -> x
555- | _ -> Intersection { types = types }
570+ | _ -> Intersection ( normalizeIntersection { types = types })
556571
557572 let substTypeVarInInheritingType subst ctx = function
558573 | InheritingType.KnownIdent x ->
@@ -694,7 +709,7 @@ module Type =
694709 let members = c.members |> List.map snd
695710 let intersection = function
696711 | [] -> None
697- | ts -> createIntersection ctx ts |> Some
712+ | ts -> createIntersection ts |> Some
698713 let rec go = function
699714 | TypeLiteral ( LString name) ->
700715 let funcs , others =
@@ -815,7 +830,7 @@ module Type =
815830 | _ -> None)
816831 match types with
817832 | [] -> onFail ()
818- | _ -> createIntersection ctx types
833+ | _ -> createIntersection types
819834
820835 | Keyof t ->
821836 let t = resolveErasedTypeImpl typeQueries ctx t
0 commit comments