From 21f61c00023314162caa5c65fcd731edf25dbae8 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 2 Jan 2021 11:37:13 -1000 Subject: [PATCH] Recursively extract named union types --- src/compiler/checker.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 696b92a0a5d..204a563f31a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13249,6 +13249,20 @@ namespace ts { return !!(type.flags & TypeFlags.Union && (type.aliasSymbol || (type).origin)); } + function addNamedUnions(namedUnions: Type[], types: readonly Type[]) { + for (const t of types) { + if (t.flags & TypeFlags.Union) { + const origin = (t).origin; + if (t.aliasSymbol || origin && !(origin.flags & TypeFlags.Union)) { + namedUnions.push(t); + } + else if (origin && origin.flags & TypeFlags.Union) { + addNamedUnions(namedUnions, (origin).types); + } + } + } + } + // We sort and deduplicate the constituent types based on object identity. If the subtypeReduction // flag is specified we also reduce the constituent type set to only include types that aren't subtypes // of other types. Subtype reduction is expensive for large union types and is possible only when union @@ -13291,7 +13305,8 @@ namespace ts { } } if (!origin && includes & TypeFlags.Union) { - const namedUnions = filter(types, isNamedUnionType); + const namedUnions: Type[] = []; + addNamedUnions(namedUnions, types); const reducedTypes: Type[] = []; for (const t of typeSet) { if (!isNamedUnionType(t) && !some(namedUnions, union => containsType((union).types, t))) {