Skip to content

Commit ea3fe85

Browse files
committed
detect useless narrowing when narrowed type is simply type parameter
1 parent a3a54ce commit ea3fe85

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44158,15 +44158,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4415844158
getNodeLinks(narrowReference).resolvedSymbol = symbol;
4415944159
setParent(narrowReference, narrowPosition.parent);
4416044160
setNodeFlags(narrowReference, narrowReference.flags | NodeFlags.Synthesized);
44161-
// >> TODO: get rid of this cast?
4416244161
narrowReference.flowNode = narrowPosition.flowNode;
4416344162
const exprType = getTypeOfExpression(narrowReference);
44163+
// Try to detect if the type of the reference was not narrowed.
4416444164
// >> TODO: is there a better way of detecting that narrowing will be useless?
44165-
if (getConstraintOfTypeParameter(tp)) {
44166-
const narrowableConstraintType = mapType(tp.constraint!, getBaseConstraintOrType);
44167-
if (narrowableConstraintType === exprType) {
44168-
return undefined; // Don't narrow if narrowing didn't do anything but default to constraints
44169-
}
44165+
if (exprType === tp || exprType === mapType(tp, getBaseConstraintOrType)) {
44166+
return undefined; // Don't narrow if narrowing didn't do anything but default to the type parameter or its constraint.
4417044167
}
4417144168
return [tp, exprType];
4417244169
});

0 commit comments

Comments
 (0)