Skip to content

Commit d60667e

Browse files
committed
Factor empty intersection check into function
1 parent 2090582 commit d60667e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10058,6 +10058,13 @@ namespace ts {
1005810058
return type.flags & TypeFlags.Substitution ? (<SubstitutionType>type).typeVariable : type;
1005910059
}
1006010060

10061+
/**
10062+
* Invokes union simplification logic to determine if an intersection is considered empty as a union constituent
10063+
*/
10064+
function isIntersectionEmpty(type1: Type, type2: Type) {
10065+
return !!(getUnionType([intersectTypes(type1, type2), neverType]).flags & TypeFlags.Never);
10066+
}
10067+
1006110068
function getConditionalType(root: ConditionalRoot, mapper: TypeMapper | undefined): Type {
1006210069
const checkType = instantiateType(root.checkType, mapper);
1006310070
const extendsType = instantiateType(root.extendsType, mapper);
@@ -10076,15 +10083,15 @@ namespace ts {
1007610083
if (checkType.flags & TypeFlags.Any || isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(extendsType))) { // Always true
1007710084
result = getDefaultConstraintOfTrueBranchOfConditionalType(root, /*combinedMapper*/ undefined, mapper);
1007810085
}
10079-
else if (getUnionType([getIntersectionType([checkType, extendsType]), neverType]).flags & TypeFlags.Never) { // Always false
10086+
else if (isIntersectionEmpty(checkType, extendsType)) { // Always false
1008010087
result = neverType;
1008110088
}
1008210089
}
1008310090
else if (trueType.flags & TypeFlags.Never && isTypeIdenticalTo(getActualTypeVariable(falseType), getActualTypeVariable(checkType))) {
1008410091
if (!(checkType.flags & TypeFlags.Any) && isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(extendsType))) { // Always true
1008510092
result = neverType;
1008610093
}
10087-
else if (checkType.flags & TypeFlags.Any || getUnionType([getIntersectionType([checkType, extendsType]), neverType]).flags & TypeFlags.Never) { // Always false
10094+
else if (checkType.flags & TypeFlags.Any || isIntersectionEmpty(checkType, extendsType)) { // Always false
1008810095
result = falseType; // TODO: Intersect negated `extends` type here
1008910096
}
1009010097
}

0 commit comments

Comments
 (0)