Skip to content

fix(46909):Fix typePredicate in UnionType #47172

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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24735,7 +24735,15 @@ namespace ts {
// If the current type is a union type, remove all constituents that couldn't be instances of
// the candidate type. If one or more constituents remain, return a union of those.
if (type.flags & TypeFlags.Union) {
const assignableType = filterType(type, t => isRelated(t, candidate));
let assignableType = filterType(type, t => isRelated(t, candidate));
if(candidate.flags & TypeFlags.Union){
const unionAssignableType = mapType(type, type => {
return filterType(candidate, candidate => isRelated(candidate, type));
});
if (!isTypeSubtypeOf(unionAssignableType, assignableType)) {
assignableType = unionAssignableType;
}
Comment on lines +24743 to +24745
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This branch is included because even if the types of assignableType and unionAssignableType are the same, the symbols change.
If there is a more appropriate function than isTypeSubtypeOf, I would like to use it.

}
if (!(assignableType.flags & TypeFlags.Never)) {
return assignableType;
}
Expand Down
68 changes: 68 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//// [typePredicatesInUnion3.ts]
function test1(x: string | 0) {
if (check1(x)) {
x
}
if (check2(x)) {
x
}
}

function test2(x: string | number) {
if (check1(x)) {
x
}
if (check2(x)) {
x
}
}


function test3(x: "hello" | number) {
if (check1(x)) {
x
}
if (check2(x)) {
x
}
}

function check1(x: string | number): x is ("hello" | 0) {
return x === "hello" || x === 0;
}

function check2(x: string | number): x is ("hello" | "bye" | 0) {
return x === "hello" || x === "bye" || x === 0;
}

//// [typePredicatesInUnion3.js]
function test1(x) {
if (check1(x)) {
x;
}
if (check2(x)) {
x;
}
}
function test2(x) {
if (check1(x)) {
x;
}
if (check2(x)) {
x;
}
}
function test3(x) {
if (check1(x)) {
x;
}
if (check2(x)) {
x;
}
}
function check1(x) {
return x === "hello" || x === 0;
}
function check2(x) {
return x === "hello" || x === "bye" || x === 0;
}
82 changes: 82 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion3.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
=== tests/cases/compiler/typePredicatesInUnion3.ts ===
function test1(x: string | 0) {
>test1 : Symbol(test1, Decl(typePredicatesInUnion3.ts, 0, 0))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 0, 15))

if (check1(x)) {
>check1 : Symbol(check1, Decl(typePredicatesInUnion3.ts, 26, 1))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 0, 15))

x
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 0, 15))
}
if (check2(x)) {
>check2 : Symbol(check2, Decl(typePredicatesInUnion3.ts, 30, 1))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 0, 15))

x
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 0, 15))
}
}

function test2(x: string | number) {
>test2 : Symbol(test2, Decl(typePredicatesInUnion3.ts, 7, 1))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 9, 15))

if (check1(x)) {
>check1 : Symbol(check1, Decl(typePredicatesInUnion3.ts, 26, 1))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 9, 15))

x
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 9, 15))
}
if (check2(x)) {
>check2 : Symbol(check2, Decl(typePredicatesInUnion3.ts, 30, 1))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 9, 15))

x
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 9, 15))
}
}


function test3(x: "hello" | number) {
>test3 : Symbol(test3, Decl(typePredicatesInUnion3.ts, 16, 1))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 19, 15))

if (check1(x)) {
>check1 : Symbol(check1, Decl(typePredicatesInUnion3.ts, 26, 1))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 19, 15))

x
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 19, 15))
}
if (check2(x)) {
>check2 : Symbol(check2, Decl(typePredicatesInUnion3.ts, 30, 1))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 19, 15))

x
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 19, 15))
}
}

function check1(x: string | number): x is ("hello" | 0) {
>check1 : Symbol(check1, Decl(typePredicatesInUnion3.ts, 26, 1))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 28, 16))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 28, 16))

return x === "hello" || x === 0;
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 28, 16))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 28, 16))
}

function check2(x: string | number): x is ("hello" | "bye" | 0) {
>check2 : Symbol(check2, Decl(typePredicatesInUnion3.ts, 30, 1))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 32, 16))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 32, 16))

return x === "hello" || x === "bye" || x === 0;
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 32, 16))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 32, 16))
>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 32, 16))
}
99 changes: 99 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion3.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
=== tests/cases/compiler/typePredicatesInUnion3.ts ===
function test1(x: string | 0) {
>test1 : (x: string | 0) => void
>x : string | 0

if (check1(x)) {
>check1(x) : boolean
>check1 : (x: string | number) => x is 0 | "hello"
>x : string | 0

x
>x : 0 | "hello"
}
if (check2(x)) {
>check2(x) : boolean
>check2 : (x: string | number) => x is 0 | "hello" | "bye"
>x : string | 0

x
>x : 0 | "hello" | "bye"
}
}

function test2(x: string | number) {
>test2 : (x: string | number) => void
>x : string | number

if (check1(x)) {
>check1(x) : boolean
>check1 : (x: string | number) => x is 0 | "hello"
>x : string | number

x
>x : 0 | "hello"
}
if (check2(x)) {
>check2(x) : boolean
>check2 : (x: string | number) => x is 0 | "hello" | "bye"
>x : string | number

x
>x : 0 | "hello" | "bye"
}
}


function test3(x: "hello" | number) {
>test3 : (x: "hello" | number) => void
>x : number | "hello"

if (check1(x)) {
>check1(x) : boolean
>check1 : (x: string | number) => x is 0 | "hello"
>x : number | "hello"

x
>x : 0 | "hello"
}
if (check2(x)) {
>check2(x) : boolean
>check2 : (x: string | number) => x is 0 | "hello" | "bye"
>x : number | "hello"

x
>x : 0 | "hello"
}
}

function check1(x: string | number): x is ("hello" | 0) {
>check1 : (x: string | number) => x is 0 | "hello"
>x : string | number

return x === "hello" || x === 0;
>x === "hello" || x === 0 : boolean
>x === "hello" : boolean
>x : string | number
>"hello" : "hello"
>x === 0 : boolean
>x : string | number
>0 : 0
}

function check2(x: string | number): x is ("hello" | "bye" | 0) {
>check2 : (x: string | number) => x is 0 | "hello" | "bye"
>x : string | number

return x === "hello" || x === "bye" || x === 0;
>x === "hello" || x === "bye" || x === 0 : boolean
>x === "hello" || x === "bye" : boolean
>x === "hello" : boolean
>x : string | number
>"hello" : "hello"
>x === "bye" : boolean
>x : string | number
>"bye" : "bye"
>x === 0 : boolean
>x : string | number
>0 : 0
}
35 changes: 35 additions & 0 deletions tests/cases/compiler/typePredicatesInUnion3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function test1(x: string | 0) {
if (check1(x)) {
x
}
if (check2(x)) {
x
}
}

function test2(x: string | number) {
if (check1(x)) {
x
}
if (check2(x)) {
x
}
}


function test3(x: "hello" | number) {
if (check1(x)) {
x
}
if (check2(x)) {
x
}
}

function check1(x: string | number): x is ("hello" | 0) {
return x === "hello" || x === 0;
}

function check2(x: string | number): x is ("hello" | "bye" | 0) {
return x === "hello" || x === "bye" || x === 0;
}