Skip to content

Favor asserted type in type predicate narrowing #50044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25428,8 +25428,12 @@ namespace ts {
const discriminant = keyPropertyName && getTypeOfPropertyOfType(c, keyPropertyName);
const matching = discriminant && getConstituentTypeForKeyType(type as UnionType, discriminant);
// For each constituent t in the current type, if t and and c are directly related, pick the most
// specific of the two.
const directlyRelated = mapType(matching || type, t => isRelated(t, c) ? t : isRelated(c, t) ? c : neverType);
// specific of the two. When t and c are related in both directions, we prefer c for type predicates
// because that is the asserted type, but t for `instanceof` because generics aren't reflected in
// prototype object types.
const directlyRelated = mapType(matching || type, checkDerived ?
t => isTypeDerivedFrom(t, c) ? t : isTypeDerivedFrom(c, t) ? c : neverType :
Copy link
Member

@DanielRosenwasser DanielRosenwasser Jul 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need the new isTypeDerivedFrom checks too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isTypeDerivedFrom check isn't new. It's just that I'm calling it directly instead of indirectly through the isRelated local. Once we know checkDerived is true, we also know that isRelated references isTypeDerivedFrom, so might as well call it directly.

t => isTypeSubtypeOf(c, t) ? c : isTypeSubtypeOf(t, c) ? t : neverType);
// If no constituents are directly related, create intersections for any generic constituents that
// are related by constraint.
return directlyRelated.flags & TypeFlags.Never ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ if (isNodeList(sourceObj)) {
>sourceObj : Symbol(sourceObj, Decl(controlFlowBinaryOrExpression.ts, 23, 3))

sourceObj.length;
>sourceObj.length : Symbol(length, Decl(controlFlowBinaryOrExpression.ts, 10, 27), Decl(controlFlowBinaryOrExpression.ts, 14, 33))
>sourceObj.length : Symbol(NodeList.length, Decl(controlFlowBinaryOrExpression.ts, 10, 27))
>sourceObj : Symbol(sourceObj, Decl(controlFlowBinaryOrExpression.ts, 23, 3))
>length : Symbol(length, Decl(controlFlowBinaryOrExpression.ts, 10, 27), Decl(controlFlowBinaryOrExpression.ts, 14, 33))
>length : Symbol(NodeList.length, Decl(controlFlowBinaryOrExpression.ts, 10, 27))
}

if (isHTMLCollection(sourceObj)) {
>isHTMLCollection : Symbol(isHTMLCollection, Decl(controlFlowBinaryOrExpression.ts, 18, 67))
>sourceObj : Symbol(sourceObj, Decl(controlFlowBinaryOrExpression.ts, 23, 3))

sourceObj.length;
>sourceObj.length : Symbol(length, Decl(controlFlowBinaryOrExpression.ts, 10, 27), Decl(controlFlowBinaryOrExpression.ts, 14, 33))
>sourceObj.length : Symbol(HTMLCollection.length, Decl(controlFlowBinaryOrExpression.ts, 14, 33))
>sourceObj : Symbol(sourceObj, Decl(controlFlowBinaryOrExpression.ts, 23, 3))
>length : Symbol(length, Decl(controlFlowBinaryOrExpression.ts, 10, 27), Decl(controlFlowBinaryOrExpression.ts, 14, 33))
>length : Symbol(HTMLCollection.length, Decl(controlFlowBinaryOrExpression.ts, 14, 33))
}

if (isNodeList(sourceObj) || isHTMLCollection(sourceObj)) {
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/controlFlowBinaryOrExpression.types
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,26 @@ if (isNodeList(sourceObj)) {

sourceObj.length;
>sourceObj.length : number
>sourceObj : NodeList | HTMLCollection
>sourceObj : NodeList
>length : number
}

if (isHTMLCollection(sourceObj)) {
>isHTMLCollection(sourceObj) : boolean
>isHTMLCollection : (sourceObj: any) => sourceObj is HTMLCollection
>sourceObj : EventTargetLike
>sourceObj : NodeList | { a: string; }

sourceObj.length;
>sourceObj.length : number
>sourceObj : NodeList | HTMLCollection
>sourceObj : HTMLCollection
>length : number
}

if (isNodeList(sourceObj) || isHTMLCollection(sourceObj)) {
>isNodeList(sourceObj) || isHTMLCollection(sourceObj) : boolean
>isNodeList(sourceObj) : boolean
>isNodeList : (sourceObj: any) => sourceObj is NodeList
>sourceObj : EventTargetLike
>sourceObj : HTMLCollection | { a: string; }
>isHTMLCollection(sourceObj) : boolean
>isHTMLCollection : (sourceObj: any) => sourceObj is HTMLCollection
>sourceObj : { a: string; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ function foo2(x: C1 | C2 | C3): string {
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 23, 14))

return x.item;
>x.item : Symbol(item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 10), Decl(instanceofWithStructurallyIdenticalTypes.ts, 4, 10))
>x.item : Symbol(C1.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 10))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 23, 14))
>item : Symbol(item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 10), Decl(instanceofWithStructurallyIdenticalTypes.ts, 4, 10))
>item : Symbol(C1.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 10))
}
else if (isC2(x)) {
>isC2 : Symbol(isC2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 19, 66))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function foo2(x: C1 | C2 | C3): string {

return x.item;
>x.item : string
>x : C1 | C3
>x : C1
>item : string
}
else if (isC2(x)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ function getResults1(value: Results | { data: Results }): Results {
return isResponseInData(value) ? value.data : value;
>isResponseInData : Symbol(isResponseInData, Decl(typePredicateStructuralMatch.ts, 9, 24))
>value : Symbol(value, Decl(typePredicateStructuralMatch.ts, 15, 21))
>value.data : Symbol(data, Decl(typePredicateStructuralMatch.ts, 15, 39))
>value.data : Symbol(data, Decl(typePredicateStructuralMatch.ts, 11, 63))
>value : Symbol(value, Decl(typePredicateStructuralMatch.ts, 15, 21))
>data : Symbol(data, Decl(typePredicateStructuralMatch.ts, 15, 39))
>data : Symbol(data, Decl(typePredicateStructuralMatch.ts, 11, 63))
>value : Symbol(value, Decl(typePredicateStructuralMatch.ts, 15, 21))
}

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/unionWithIndexSignature.types
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function flatten<T extends number|TypedArray>(arr: T) {

arr[1];
>arr[1] : number
>arr : TypedArray
>arr : Int32Array | Uint8Array
>1 : 1
}
}
Expand Down