Skip to content

Narrow on else branch of multi-conditional guard #5427

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 2 commits 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
12 changes: 6 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6395,22 +6395,22 @@ namespace ts {
return narrowType(narrowType(type, expr.left, /*assumeTrue*/ true), expr.right, /*assumeTrue*/ true);
}
else {
// The assumed result is false. This means either the first operand was false, or the first operand was true
// and the second operand was false. We narrow with those assumptions and union the two resulting types.
// The assumed result is false. This means either the first operand was false, or
// the second operand was false. We narrow with those assumptions and union the two resulting types.
return getUnionType([
narrowType(type, expr.left, /*assumeTrue*/ false),
narrowType(narrowType(type, expr.left, /*assumeTrue*/ true), expr.right, /*assumeTrue*/ false)
narrowType(type, expr.right, /*assumeTrue*/ false)
]);
}
}

function narrowTypeByOr(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type {
if (assumeTrue) {
// The assumed result is true. This means either the first operand was true, or the first operand was false
// and the second operand was true. We narrow with those assumptions and union the two resulting types.
// The assumed result is true. This means either the first operand was true, or
// the second operand was true. We narrow with those assumptions and union the two resulting types.
return getUnionType([
narrowType(type, expr.left, /*assumeTrue*/ true),
narrowType(narrowType(type, expr.left, /*assumeTrue*/ false), expr.right, /*assumeTrue*/ true)
narrowType(type, expr.right, /*assumeTrue*/ true)
]);
}
else {
Expand Down
17 changes: 17 additions & 0 deletions tests/baselines/reference/typeGuardDuplicateGuards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [typeGuardDuplicateGuards.ts]
var x: string|number;

var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;

var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;

var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;

var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr;

//// [typeGuardDuplicateGuards.js]
var x;
var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;
var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;
var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;
var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr;
48 changes: 48 additions & 0 deletions tests/baselines/reference/typeGuardDuplicateGuards.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardDuplicateGuards.ts ===
var x: string|number;
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))

var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;
>r1 : Symbol(r1, Decl(typeGuardDuplicateGuards.ts, 2, 3))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))

var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;
>r2 : Symbol(r2, Decl(typeGuardDuplicateGuards.ts, 4, 3))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>substr : Symbol(String.substr, Decl(lib.d.ts, --, --))

var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;
>r3 : Symbol(r3, Decl(typeGuardDuplicateGuards.ts, 6, 3))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))

var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr;
>r4 : Symbol(r4, Decl(typeGuardDuplicateGuards.ts, 8, 3))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardDuplicateGuards.ts, 0, 3))
>substr : Symbol(String.substr, Decl(lib.d.ts, --, --))

84 changes: 84 additions & 0 deletions tests/baselines/reference/typeGuardDuplicateGuards.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardDuplicateGuards.ts ===
var x: string|number;
>x : string | number

var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;
>r1 : (from: number, length?: number) => string
>typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed : (from: number, length?: number) => string
>typeof x === "string" && typeof x === "string" : boolean
>typeof x === "string" : boolean
>typeof x : string
>x : string | number
>"string" : string
>typeof x === "string" : boolean
>typeof x : string
>x : string
>"string" : string
>x.substr : (from: number, length?: number) => string
>x : string
>substr : (from: number, length?: number) => string
>x.toFixed : (fractionDigits?: number) => string
>x : number
>toFixed : (fractionDigits?: number) => string

var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;
>r2 : (fractionDigits?: number) => string
>!(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr : (fractionDigits?: number) => string
>!(typeof x === "string" && typeof x === "string") : boolean
>(typeof x === "string" && typeof x === "string") : boolean
>typeof x === "string" && typeof x === "string" : boolean
>typeof x === "string" : boolean
>typeof x : string
>x : string | number
>"string" : string
>typeof x === "string" : boolean
>typeof x : string
>x : string
>"string" : string
>x.toFixed : (fractionDigits?: number) => string
>x : number
>toFixed : (fractionDigits?: number) => string
>x.substr : (from: number, length?: number) => string
>x : string
>substr : (from: number, length?: number) => string

var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;
>r3 : (from: number, length?: number) => string
>typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed : (from: number, length?: number) => string
>typeof x === "string" || typeof x === "string" : boolean
>typeof x === "string" : boolean
>typeof x : string
>x : string | number
>"string" : string
>typeof x === "string" : boolean
>typeof x : string
>x : number
>"string" : string
>x.substr : (from: number, length?: number) => string
>x : string
>substr : (from: number, length?: number) => string
>x.toFixed : (fractionDigits?: number) => string
>x : number
>toFixed : (fractionDigits?: number) => string

var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr;
>r4 : (fractionDigits?: number) => string
>!(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr : (fractionDigits?: number) => string
>!(typeof x === "string" || typeof x === "string") : boolean
>(typeof x === "string" || typeof x === "string") : boolean
>typeof x === "string" || typeof x === "string" : boolean
>typeof x === "string" : boolean
>typeof x : string
>x : string | number
>"string" : string
>typeof x === "string" : boolean
>typeof x : string
>x : number
>"string" : string
>x.toFixed : (fractionDigits?: number) => string
>x : number
>toFixed : (fractionDigits?: number) => string
>x.substr : (from: number, length?: number) => string
>x : string
>substr : (from: number, length?: number) => string

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var x: string|number;

var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;

var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;

var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;

var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr;