Skip to content

Narrow unions to the narrowest constituent on assignment #47975

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22879,6 +22879,19 @@ namespace ts {
if (assignedType.flags & TypeFlags.BooleanLiteral && isFreshLiteralType(assignedType)) {
reducedType = mapType(reducedType, getFreshTypeOfLiteralType); // Ensure that if the assignment is a fresh type, that we narrow to fresh types
}
// try to narrow to the narrowest constituent.
if (reducedType.flags & TypeFlags.Union) {
reducedType = filterType(
reducedType,
(t) =>
!some((reducedType as UnionType).types, (s) => {
if (s === t || !isTypeSubtypeOf(s, t)) {
return false;
}
return everyType(assignedType, type => !(typeMaybeAssignableTo(type, t) && !typeMaybeAssignableTo(type, s)));
})
);
}
// Our crude heuristic produces an invalid result in some cases: see GH#26130.
// For now, when that happens, we give up and don't narrow at all. (This also
// means we'll never narrow for erroneous assignments where the assigned type
Expand Down
68 changes: 66 additions & 2 deletions tests/baselines/reference/controlFlowAssignmentExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,56 @@ declare function fn(): D;
let o: D;
if ((o = fn()).done) {
const y: 1 = o.value;
}
}

// https://github.com/microsoft/TypeScript/issues/47731
declare let a: object | any[] | undefined

if (a === undefined) {
a = []
} else if (!Array.isArray(a)) {
throw new Error()
}
[...a] // any[]

interface Parent {
parent: string;
}
interface Child extends Parent {
child: string;
}

declare let p: Parent;
declare let c: Child;
declare let y: Parent | Child | undefined;

y = p;
y; // Parent

y = c;
y; // Child

y = undefined as any as Parent | Child;
y; // Parent | Child

y = undefined as any as Parent | undefined;
y; // Parent | undefined

y = undefined as any as Child | undefined;
y; // Child | undefined


//// [controlFlowAssignmentExpression.js]
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var x;
var obj;
x = "";
Expand All @@ -29,5 +76,22 @@ x = true;
x; // number
var o;
if ((o = fn()).done) {
var y = o.value;
var y_1 = o.value;
}
if (a === undefined) {
a = [];
}
else if (!Array.isArray(a)) {
throw new Error();
}
__spreadArray([], a, true); // any[]
y = p;
y; // Parent
y = c;
y; // Child
y = undefined;
y; // Parent | Child
y = undefined;
y; // Parent | undefined
y = undefined;
y; // Child | undefined
90 changes: 90 additions & 0 deletions tests/baselines/reference/controlFlowAssignmentExpression.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,93 @@ if ((o = fn()).done) {
>o : Symbol(o, Decl(controlFlowAssignmentExpression.ts, 14, 3))
>value : Symbol(value, Decl(controlFlowAssignmentExpression.ts, 12, 22))
}

// https://github.com/microsoft/TypeScript/issues/47731
declare let a: object | any[] | undefined
>a : Symbol(a, Decl(controlFlowAssignmentExpression.ts, 20, 11))

if (a === undefined) {
>a : Symbol(a, Decl(controlFlowAssignmentExpression.ts, 20, 11))
>undefined : Symbol(undefined)

a = []
>a : Symbol(a, Decl(controlFlowAssignmentExpression.ts, 20, 11))

} else if (!Array.isArray(a)) {
>Array.isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --))
>a : Symbol(a, Decl(controlFlowAssignmentExpression.ts, 20, 11))

throw new Error()
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
}
[...a] // any[]
>a : Symbol(a, Decl(controlFlowAssignmentExpression.ts, 20, 11))

interface Parent {
>Parent : Symbol(Parent, Decl(controlFlowAssignmentExpression.ts, 27, 6))

parent: string;
>parent : Symbol(Parent.parent, Decl(controlFlowAssignmentExpression.ts, 29, 18))
}
interface Child extends Parent {
>Child : Symbol(Child, Decl(controlFlowAssignmentExpression.ts, 31, 1))
>Parent : Symbol(Parent, Decl(controlFlowAssignmentExpression.ts, 27, 6))

child: string;
>child : Symbol(Child.child, Decl(controlFlowAssignmentExpression.ts, 32, 32))
}

declare let p: Parent;
>p : Symbol(p, Decl(controlFlowAssignmentExpression.ts, 36, 11))
>Parent : Symbol(Parent, Decl(controlFlowAssignmentExpression.ts, 27, 6))

declare let c: Child;
>c : Symbol(c, Decl(controlFlowAssignmentExpression.ts, 37, 11))
>Child : Symbol(Child, Decl(controlFlowAssignmentExpression.ts, 31, 1))

declare let y: Parent | Child | undefined;
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))
>Parent : Symbol(Parent, Decl(controlFlowAssignmentExpression.ts, 27, 6))
>Child : Symbol(Child, Decl(controlFlowAssignmentExpression.ts, 31, 1))

y = p;
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))
>p : Symbol(p, Decl(controlFlowAssignmentExpression.ts, 36, 11))

y; // Parent
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))

y = c;
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))
>c : Symbol(c, Decl(controlFlowAssignmentExpression.ts, 37, 11))

y; // Child
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))

y = undefined as any as Parent | Child;
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))
>undefined : Symbol(undefined)
>Parent : Symbol(Parent, Decl(controlFlowAssignmentExpression.ts, 27, 6))
>Child : Symbol(Child, Decl(controlFlowAssignmentExpression.ts, 31, 1))

y; // Parent | Child
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))

y = undefined as any as Parent | undefined;
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))
>undefined : Symbol(undefined)
>Parent : Symbol(Parent, Decl(controlFlowAssignmentExpression.ts, 27, 6))

y; // Parent | undefined
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))

y = undefined as any as Child | undefined;
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))
>undefined : Symbol(undefined)
>Child : Symbol(Child, Decl(controlFlowAssignmentExpression.ts, 31, 1))

y; // Child | undefined
>y : Symbol(y, Decl(controlFlowAssignmentExpression.ts, 38, 11))

96 changes: 96 additions & 0 deletions tests/baselines/reference/controlFlowAssignmentExpression.types
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,99 @@ if ((o = fn()).done) {
>o : { done: true; value: 1; }
>value : 1
}

// https://github.com/microsoft/TypeScript/issues/47731
declare let a: object | any[] | undefined
>a : object | any[] | undefined

if (a === undefined) {
>a === undefined : boolean
>a : object | any[] | undefined
>undefined : undefined

a = []
>a = [] : never[]
>a : object | any[] | undefined
>[] : never[]

} else if (!Array.isArray(a)) {
>!Array.isArray(a) : boolean
>Array.isArray(a) : boolean
>Array.isArray : (arg: any) => arg is any[]
>Array : ArrayConstructor
>isArray : (arg: any) => arg is any[]
>a : object | any[]

throw new Error()
>new Error() : Error
>Error : ErrorConstructor
}
[...a] // any[]
>[...a] : any[]
>...a : any
>a : any[]

interface Parent {
parent: string;
>parent : string
}
interface Child extends Parent {
child: string;
>child : string
}

declare let p: Parent;
>p : Parent

declare let c: Child;
>c : Child

declare let y: Parent | Child | undefined;
>y : Parent | Child | undefined

y = p;
>y = p : Parent
>y : Parent | Child | undefined
>p : Parent

y; // Parent
>y : Parent

y = c;
>y = c : Child
>y : Parent | Child | undefined
>c : Child

y; // Child
>y : Child

y = undefined as any as Parent | Child;
>y = undefined as any as Parent | Child : Parent | Child
>y : Parent | Child | undefined
>undefined as any as Parent | Child : Parent | Child
>undefined as any : any
>undefined : undefined

y; // Parent | Child
>y : Parent | Child

y = undefined as any as Parent | undefined;
>y = undefined as any as Parent | undefined : Parent | undefined
>y : Parent | Child | undefined
>undefined as any as Parent | undefined : Parent | undefined
>undefined as any : any
>undefined : undefined

y; // Parent | undefined
>y : Parent | undefined

y = undefined as any as Child | undefined;
>y = undefined as any as Child | undefined : Child | undefined
>y : Parent | Child | undefined
>undefined as any as Child | undefined : Child | undefined
>undefined as any : any
>undefined : undefined

y; // Child | undefined
>y : Child | undefined

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
12 changes: 6 additions & 6 deletions tests/baselines/reference/controlFlowBinaryOrExpression.types
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,37 @@ var sourceObj: EventTargetLike = <any>undefined;
if (isNodeList(sourceObj)) {
>isNodeList(sourceObj) : boolean
>isNodeList : (sourceObj: any) => sourceObj is NodeList
>sourceObj : EventTargetLike
>sourceObj : { a: string; }

sourceObj.length;
>sourceObj.length : number
>sourceObj : NodeList | HTMLCollection
>sourceObj : { a: string; } & NodeList
>length : number
}

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

sourceObj.length;
>sourceObj.length : number
>sourceObj : NodeList | HTMLCollection
>sourceObj : { a: string; } & 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 : { a: string; }
>isHTMLCollection(sourceObj) : boolean
>isHTMLCollection : (sourceObj: any) => sourceObj is HTMLCollection
>sourceObj : { a: string; }

sourceObj.length;
>sourceObj.length : number
>sourceObj : NodeList
>sourceObj : { a: string; } & NodeList
>length : number
}

Loading