Narrow references within optional chains in for-in loops (#52059)

This commit is contained in:
Mateusz Burzyński
2023-02-06 16:15:27 -08:00
committed by GitHub
parent 4775381811
commit 40208a8ca7
6 changed files with 158 additions and 3 deletions
+5 -1
View File
@@ -26273,7 +26273,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return declaredType;
}
// for (const _ in ref) acts as a nonnull on ref
if (isVariableDeclaration(node) && node.parent.parent.kind === SyntaxKind.ForInStatement && isMatchingReference(reference, node.parent.parent.expression)) {
if (
isVariableDeclaration(node) &&
node.parent.parent.kind === SyntaxKind.ForInStatement &&
(isMatchingReference(reference, node.parent.parent.expression) || optionalChainContainsReference(node.parent.parent.expression, reference))
) {
return getNonNullableTypeIfNeeded(finalizeEvolvingArrayType(getTypeFromFlowType(getTypeAtFlowNode(flow.antecedent))));
}
// Assignment doesn't affect reference