From d90d6b9277953812d741b5fd43f097ea59d5d247 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 24 Apr 2018 15:55:15 -0700 Subject: [PATCH] Remove more intersections with empty value domains from union types --- src/compiler/checker.ts | 15 ++++++++++++--- src/compiler/types.ts | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8d4301c8722..0618abe75bb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7917,8 +7917,13 @@ namespace ts { return binarySearch(types, type, getTypeId, compareValues) >= 0; } - // Return true if the given intersection type contains (a) more than one unit type or (b) an object - // type and a nullable type (null or undefined). + // Return true if the given intersection type contains + // more than one unit type or, + // an object type and a nullable type (null or undefined), or + // a string-like type and a non-string-like primitive type, or + // a number-like type and a non-number-like primitive type, or + // a symbol-like type and a non-symbol-like primitive type, or + // a void-like type and a non-void-like primitive type. function isEmptyIntersectionType(type: IntersectionType) { let combined: TypeFlags = 0; for (const t of type.types) { @@ -7926,7 +7931,11 @@ namespace ts { return true; } combined |= t.flags; - if (combined & TypeFlags.Nullable && combined & (TypeFlags.Object | TypeFlags.NonPrimitive)) { + if (combined & TypeFlags.Nullable && combined & (TypeFlags.Object | TypeFlags.NonPrimitive) || + combined & TypeFlags.StringLike && combined & (TypeFlags.Primitive & ~TypeFlags.StringLike) || + combined & TypeFlags.NumberLike && combined & (TypeFlags.Primitive & ~TypeFlags.NumberLike) || + combined & TypeFlags.ESSymbolLike && combined & (TypeFlags.Primitive & ~TypeFlags.ESSymbolLike) || + combined & TypeFlags.VoidLike && combined & (TypeFlags.Primitive & ~TypeFlags.VoidLike)) { return true; } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 06f15e1d77a..fd3a58bc5ea 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3613,6 +3613,7 @@ namespace ts { BooleanLike = Boolean | BooleanLiteral, EnumLike = Enum | EnumLiteral, ESSymbolLike = ESSymbol | UniqueESSymbol, + VoidLike = Void | Undefined, UnionOrIntersection = Union | Intersection, StructuredType = Object | Union | Intersection, TypeVariable = TypeParameter | IndexedAccess,