Skip to content

Commit 91b658d

Browse files
committed
Construct intersection types for type guards involving type parameters
1 parent 4c71a7c commit 91b658d

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9843,9 +9843,8 @@ namespace ts {
98439843
if (flags & TypeFlags.NonPrimitive) {
98449844
return strictNullChecks ? TypeFacts.ObjectStrictFacts : TypeFacts.ObjectFacts;
98459845
}
9846-
if (flags & TypeFlags.TypeParameter) {
9847-
const constraint = getConstraintOfTypeParameter(<TypeParameter>type);
9848-
return getTypeFacts(constraint || emptyObjectType);
9846+
if (flags & TypeFlags.TypeVariable) {
9847+
return getTypeFacts(getBaseConstraintOfType(<TypeVariable>type) || emptyObjectType);
98499848
}
98509849
if (flags & TypeFlags.UnionOrIntersection) {
98519850
return getTypeFactsOfTypes((<UnionOrIntersectionType>type).types);
@@ -10632,8 +10631,16 @@ namespace ts {
1063210631
// is a supertype of that primitive type. For example, type 'any' can be narrowed
1063310632
// to one of the primitive types.
1063410633
const targetType = typeofTypesByName.get(literal.text);
10635-
if (targetType && isTypeSubtypeOf(targetType, type)) {
10636-
return targetType;
10634+
if (targetType) {
10635+
if (isTypeSubtypeOf(targetType, type)) {
10636+
return targetType;
10637+
}
10638+
if (type.flags & TypeFlags.TypeVariable) {
10639+
const constraint = getBaseConstraintOfType(<TypeVariable>type) || anyType;
10640+
if (isTypeSubtypeOf(targetType, constraint)) {
10641+
return getIntersectionType([type, targetType]);
10642+
}
10643+
}
1063710644
}
1063810645
}
1063910646
const facts = assumeTrue ?
@@ -10731,10 +10738,9 @@ namespace ts {
1073110738
// Otherwise, if the candidate type is assignable to the target type, narrow to the candidate
1073210739
// type. Otherwise, the types are completely unrelated, so narrow to an intersection of the
1073310740
// two types.
10734-
const targetType = type.flags & TypeFlags.TypeParameter ? getApparentType(type) : type;
1073510741
return isTypeSubtypeOf(candidate, type) ? candidate :
1073610742
isTypeAssignableTo(type, candidate) ? type :
10737-
isTypeAssignableTo(candidate, targetType) ? candidate :
10743+
isTypeAssignableTo(candidate, type) ? candidate :
1073810744
getIntersectionType([type, candidate]);
1073910745
}
1074010746

0 commit comments

Comments
 (0)