Skip to content

Commit 4123068

Browse files
committed
Only get apparent type when constraint includes nullable types
1 parent 238067e commit 4123068

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11717,17 +11717,18 @@ namespace ts {
1171711717
parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>parent).expression === node;
1171811718
}
1171911719

11720+
function typeHasNullableConstraint(type: Type) {
11721+
return type.flags & TypeFlags.TypeVariable && maybeTypeOfKind(getBaseConstraintOfType(type) || emptyObjectType, TypeFlags.Nullable);
11722+
}
11723+
1172011724
function getDeclaredOrApparentType(symbol: Symbol, node: Node) {
1172111725
// When a node is the left hand expression of a property access, element access, or call expression,
1172211726
// and the type of the node includes type variables with constraints that are nullable, we fetch the
1172311727
// apparent type of the node *before* performing control flow analysis such that narrowings apply to
1172411728
// the constraint type.
1172511729
const type = getTypeOfSymbol(symbol);
11726-
if (isApparentTypePosition(node) && maybeTypeOfKind(type, TypeFlags.TypeVariable)) {
11727-
const apparentType = mapType(getWidenedType(type), getApparentType);
11728-
if (maybeTypeOfKind(apparentType, TypeFlags.Nullable)) {
11729-
return apparentType;
11730-
}
11730+
if (isApparentTypePosition(node) && forEachType(type, typeHasNullableConstraint)) {
11731+
return mapType(getWidenedType(type), getApparentType);
1173111732
}
1173211733
return type;
1173311734
}

0 commit comments

Comments
 (0)