Tests for conditional expression typeguards
• In the true expression of a conditional expression, the type of a variable or parameter is narrowed by any type guard in the condition when true, provided the expression contains no assignments to the variable or parameter. • In the false expression of a conditional expression, the type of a variable or parameter is narrowed by any type guard in the condition when false, provided the expression contains no assignments to the variable or parameter.
This commit is contained in:
parent
4b3d603f24
commit
55952af304
3 changed files with 613 additions and 0 deletions
160
tests/baselines/reference/typeGuardsInConditionalExpression.js
Normal file
160
tests/baselines/reference/typeGuardsInConditionalExpression.js
Normal file
|
@ -0,0 +1,160 @@
|
|||
//// [typeGuardsInConditionalExpression.ts]
|
||||
// In the true expression of a conditional expression,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the condition when true,
|
||||
// provided the true expression contains no assignments to the variable or parameter.
|
||||
// In the false expression of a conditional expression,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the condition when false,
|
||||
// provided the false expression contains no assignments to the variable or parameter.
|
||||
|
||||
function foo(x: number | string) {
|
||||
return typeof x === "string"
|
||||
? x.length // string
|
||||
: x++; // number
|
||||
}
|
||||
function foo2(x: number | string) {
|
||||
// x is assigned in the if true branch, the type is not narrowed
|
||||
return typeof x === "string"
|
||||
? (x = 10 && x)// string | number
|
||||
: x; // string | number
|
||||
}
|
||||
function foo3(x: number | string) {
|
||||
// x is assigned in the if false branch, the type is not narrowed
|
||||
// even though assigned using same type as narrowed expression
|
||||
return typeof x === "string"
|
||||
? (x = "Hello" && x) // string | number
|
||||
: x; // string | number
|
||||
}
|
||||
function foo4(x: number | string) {
|
||||
// false branch updates the variable - so here it is not number
|
||||
// even though assigned using same type as narrowed expression
|
||||
return typeof x === "string"
|
||||
? x // string | number
|
||||
: (x = 10 && x); // string | number
|
||||
}
|
||||
function foo5(x: number | string) {
|
||||
// false branch updates the variable - so here it is not number
|
||||
return typeof x === "string"
|
||||
? x // string | number
|
||||
: (x = "hello" && x); // string | number
|
||||
}
|
||||
function foo6(x: number | string) {
|
||||
// Modify in both branches
|
||||
return typeof x === "string"
|
||||
? (x = 10 && x) // string | number
|
||||
: (x = "hello" && x); // string | number
|
||||
}
|
||||
function foo7(x: number | string | boolean) {
|
||||
return typeof x === "string"
|
||||
? x === "hello" // string
|
||||
: typeof x === "boolean"
|
||||
? x // boolean
|
||||
: x == 10; // number
|
||||
}
|
||||
function foo8(x: number | string | boolean) {
|
||||
var b: number | boolean;
|
||||
return typeof x === "string"
|
||||
? x === "hello"
|
||||
: ((b = x) && // number | boolean
|
||||
(typeof x === "boolean"
|
||||
? x // boolean
|
||||
: x == 10)); // number
|
||||
}
|
||||
function foo9(x: number | string) {
|
||||
var y = 10;
|
||||
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
|
||||
return typeof x === "string"
|
||||
? ((y = x.length) && x === "hello") // string
|
||||
: x === 10; // number
|
||||
}
|
||||
function foo10(x: number | string | boolean) {
|
||||
// Mixing typeguards
|
||||
var b: boolean | number;
|
||||
return typeof x === "string"
|
||||
? x // string
|
||||
: ((b = x) // x is number | boolean
|
||||
&& typeof x === "number"
|
||||
&& x.toString()); // x is number
|
||||
}
|
||||
function foo11(x: number | string | boolean) {
|
||||
// Mixing typeguards
|
||||
// Assigning value to x deep inside another guard stops narrowing of type too
|
||||
var b: number | boolean | string;
|
||||
return typeof x === "string"
|
||||
? x // number | boolean | string - changed in the false branch
|
||||
: ((b = x) // x is number | boolean | string - because the assignment changed it
|
||||
&& typeof x === "number"
|
||||
&& (x = 10) // assignment to x
|
||||
&& x); // x is number | boolean | string
|
||||
}
|
||||
function foo12(x: number | string | boolean) {
|
||||
// Mixing typeguards
|
||||
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
|
||||
var b: number | boolean | string;
|
||||
return typeof x === "string"
|
||||
? (x = 10 && x.toString().length) // number | boolean | string - changed here
|
||||
: ((b = x) // x is number | boolean | string - changed in true branch
|
||||
&& typeof x === "number"
|
||||
&& x); // x is number
|
||||
}
|
||||
|
||||
//// [typeGuardsInConditionalExpression.js]
|
||||
// In the true expression of a conditional expression,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the condition when true,
|
||||
// provided the true expression contains no assignments to the variable or parameter.
|
||||
// In the false expression of a conditional expression,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the condition when false,
|
||||
// provided the false expression contains no assignments to the variable or parameter.
|
||||
function foo(x) {
|
||||
return typeof x === "string" ? x.length : x++; // number
|
||||
}
|
||||
function foo2(x) {
|
||||
// x is assigned in the if true branch, the type is not narrowed
|
||||
return typeof x === "string" ? (x = 10 && x) : x; // string | number
|
||||
}
|
||||
function foo3(x) {
|
||||
// x is assigned in the if false branch, the type is not narrowed
|
||||
// even though assigned using same type as narrowed expression
|
||||
return typeof x === "string" ? (x = "Hello" && x) : x; // string | number
|
||||
}
|
||||
function foo4(x) {
|
||||
// false branch updates the variable - so here it is not number
|
||||
// even though assigned using same type as narrowed expression
|
||||
return typeof x === "string" ? x : (x = 10 && x); // string | number
|
||||
}
|
||||
function foo5(x) {
|
||||
// false branch updates the variable - so here it is not number
|
||||
return typeof x === "string" ? x : (x = "hello" && x); // string | number
|
||||
}
|
||||
function foo6(x) {
|
||||
// Modify in both branches
|
||||
return typeof x === "string" ? (x = 10 && x) : (x = "hello" && x); // string | number
|
||||
}
|
||||
function foo7(x) {
|
||||
return typeof x === "string" ? x === "hello" : typeof x === "boolean" ? x : x == 10; // number
|
||||
}
|
||||
function foo8(x) {
|
||||
var b;
|
||||
return typeof x === "string" ? x === "hello" : ((b = x) && (typeof x === "boolean" ? x : x == 10)); // number
|
||||
}
|
||||
function foo9(x) {
|
||||
var y = 10;
|
||||
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
|
||||
return typeof x === "string" ? ((y = x.length) && x === "hello") : x === 10; // number
|
||||
}
|
||||
function foo10(x) {
|
||||
// Mixing typeguards
|
||||
var b;
|
||||
return typeof x === "string" ? x : ((b = x) && typeof x === "number" && x.toString()); // x is number
|
||||
}
|
||||
function foo11(x) {
|
||||
// Mixing typeguards
|
||||
// Assigning value to x deep inside another guard stops narrowing of type too
|
||||
var b;
|
||||
return typeof x === "string" ? x : ((b = x) && typeof x === "number" && (x = 10) && x); // x is number | boolean | string
|
||||
}
|
||||
function foo12(x) {
|
||||
// Mixing typeguards
|
||||
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
|
||||
var b;
|
||||
return typeof x === "string" ? (x = 10 && x.toString().length) : ((b = x) && typeof x === "number" && x); // x is number
|
||||
}
|
|
@ -0,0 +1,356 @@
|
|||
=== tests/cases/conformance/expressions/typeGuards/typeGuardsInConditionalExpression.ts ===
|
||||
// In the true expression of a conditional expression,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the condition when true,
|
||||
// provided the true expression contains no assignments to the variable or parameter.
|
||||
// In the false expression of a conditional expression,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the condition when false,
|
||||
// provided the false expression contains no assignments to the variable or parameter.
|
||||
|
||||
function foo(x: number | string) {
|
||||
>foo : (x: string | number) => number
|
||||
>x : string | number
|
||||
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? x.length // string : x++ : number
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number
|
||||
|
||||
? x.length // string
|
||||
>x.length : number
|
||||
>x : string
|
||||
>length : number
|
||||
|
||||
: x++; // number
|
||||
>x++ : number
|
||||
>x : number
|
||||
}
|
||||
function foo2(x: number | string) {
|
||||
>foo2 : (x: string | number) => string | number
|
||||
>x : string | number
|
||||
|
||||
// x is assigned in the if true branch, the type is not narrowed
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? (x = 10 && x)// string | number : x : string | number
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number
|
||||
|
||||
? (x = 10 && x)// string | number
|
||||
>(x = 10 && x) : string | number
|
||||
>x = 10 && x : string | number
|
||||
>x : string | number
|
||||
>10 && x : string | number
|
||||
>x : string | number
|
||||
|
||||
: x; // string | number
|
||||
>x : string | number
|
||||
}
|
||||
function foo3(x: number | string) {
|
||||
>foo3 : (x: string | number) => string | number
|
||||
>x : string | number
|
||||
|
||||
// x is assigned in the if false branch, the type is not narrowed
|
||||
// even though assigned using same type as narrowed expression
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? (x = "Hello" && x) // string | number : x : string | number
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number
|
||||
|
||||
? (x = "Hello" && x) // string | number
|
||||
>(x = "Hello" && x) : string | number
|
||||
>x = "Hello" && x : string | number
|
||||
>x : string | number
|
||||
>"Hello" && x : string | number
|
||||
>x : string | number
|
||||
|
||||
: x; // string | number
|
||||
>x : string | number
|
||||
}
|
||||
function foo4(x: number | string) {
|
||||
>foo4 : (x: string | number) => string | number
|
||||
>x : string | number
|
||||
|
||||
// false branch updates the variable - so here it is not number
|
||||
// even though assigned using same type as narrowed expression
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? x // string | number : (x = 10 && x) : string | number
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number
|
||||
|
||||
? x // string | number
|
||||
>x : string | number
|
||||
|
||||
: (x = 10 && x); // string | number
|
||||
>(x = 10 && x) : string | number
|
||||
>x = 10 && x : string | number
|
||||
>x : string | number
|
||||
>10 && x : string | number
|
||||
>x : string | number
|
||||
}
|
||||
function foo5(x: number | string) {
|
||||
>foo5 : (x: string | number) => string | number
|
||||
>x : string | number
|
||||
|
||||
// false branch updates the variable - so here it is not number
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? x // string | number : (x = "hello" && x) : string | number
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number
|
||||
|
||||
? x // string | number
|
||||
>x : string | number
|
||||
|
||||
: (x = "hello" && x); // string | number
|
||||
>(x = "hello" && x) : string | number
|
||||
>x = "hello" && x : string | number
|
||||
>x : string | number
|
||||
>"hello" && x : string | number
|
||||
>x : string | number
|
||||
}
|
||||
function foo6(x: number | string) {
|
||||
>foo6 : (x: string | number) => string | number
|
||||
>x : string | number
|
||||
|
||||
// Modify in both branches
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? (x = 10 && x) // string | number : (x = "hello" && x) : string | number
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number
|
||||
|
||||
? (x = 10 && x) // string | number
|
||||
>(x = 10 && x) : string | number
|
||||
>x = 10 && x : string | number
|
||||
>x : string | number
|
||||
>10 && x : string | number
|
||||
>x : string | number
|
||||
|
||||
: (x = "hello" && x); // string | number
|
||||
>(x = "hello" && x) : string | number
|
||||
>x = "hello" && x : string | number
|
||||
>x : string | number
|
||||
>"hello" && x : string | number
|
||||
>x : string | number
|
||||
}
|
||||
function foo7(x: number | string | boolean) {
|
||||
>foo7 : (x: string | number | boolean) => boolean
|
||||
>x : string | number | boolean
|
||||
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? x === "hello" // string : typeof x === "boolean" ? x // boolean : x == 10 : boolean
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number | boolean
|
||||
|
||||
? x === "hello" // string
|
||||
>x === "hello" : boolean
|
||||
>x : string
|
||||
|
||||
: typeof x === "boolean"
|
||||
>typeof x === "boolean" ? x // boolean : x == 10 : boolean
|
||||
>typeof x === "boolean" : boolean
|
||||
>typeof x : string
|
||||
>x : number | boolean
|
||||
|
||||
? x // boolean
|
||||
>x : boolean
|
||||
|
||||
: x == 10; // number
|
||||
>x == 10 : boolean
|
||||
>x : number
|
||||
}
|
||||
function foo8(x: number | string | boolean) {
|
||||
>foo8 : (x: string | number | boolean) => boolean
|
||||
>x : string | number | boolean
|
||||
|
||||
var b: number | boolean;
|
||||
>b : number | boolean
|
||||
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? x === "hello" : ((b = x) && // number | boolean (typeof x === "boolean" ? x // boolean : x == 10)) : boolean
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number | boolean
|
||||
|
||||
? x === "hello"
|
||||
>x === "hello" : boolean
|
||||
>x : string
|
||||
|
||||
: ((b = x) && // number | boolean
|
||||
>((b = x) && // number | boolean (typeof x === "boolean" ? x // boolean : x == 10)) : boolean
|
||||
>(b = x) && // number | boolean (typeof x === "boolean" ? x // boolean : x == 10) : boolean
|
||||
>(b = x) : number | boolean
|
||||
>b = x : number | boolean
|
||||
>b : number | boolean
|
||||
>x : number | boolean
|
||||
|
||||
(typeof x === "boolean"
|
||||
>(typeof x === "boolean" ? x // boolean : x == 10) : boolean
|
||||
>typeof x === "boolean" ? x // boolean : x == 10 : boolean
|
||||
>typeof x === "boolean" : boolean
|
||||
>typeof x : string
|
||||
>x : number | boolean
|
||||
|
||||
? x // boolean
|
||||
>x : boolean
|
||||
|
||||
: x == 10)); // number
|
||||
>x == 10 : boolean
|
||||
>x : number
|
||||
}
|
||||
function foo9(x: number | string) {
|
||||
>foo9 : (x: string | number) => boolean
|
||||
>x : string | number
|
||||
|
||||
var y = 10;
|
||||
>y : number
|
||||
|
||||
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? ((y = x.length) && x === "hello") // string : x === 10 : boolean
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number
|
||||
|
||||
? ((y = x.length) && x === "hello") // string
|
||||
>((y = x.length) && x === "hello") : boolean
|
||||
>(y = x.length) && x === "hello" : boolean
|
||||
>(y = x.length) : number
|
||||
>y = x.length : number
|
||||
>y : number
|
||||
>x.length : number
|
||||
>x : string
|
||||
>length : number
|
||||
>x === "hello" : boolean
|
||||
>x : string
|
||||
|
||||
: x === 10; // number
|
||||
>x === 10 : boolean
|
||||
>x : number
|
||||
}
|
||||
function foo10(x: number | string | boolean) {
|
||||
>foo10 : (x: string | number | boolean) => string
|
||||
>x : string | number | boolean
|
||||
|
||||
// Mixing typeguards
|
||||
var b: boolean | number;
|
||||
>b : number | boolean
|
||||
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? x // string : ((b = x) // x is number | boolean && typeof x === "number" && x.toString()) : string
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number | boolean
|
||||
|
||||
? x // string
|
||||
>x : string
|
||||
|
||||
: ((b = x) // x is number | boolean
|
||||
>((b = x) // x is number | boolean && typeof x === "number" && x.toString()) : string
|
||||
>(b = x) // x is number | boolean && typeof x === "number" && x.toString() : string
|
||||
>(b = x) // x is number | boolean && typeof x === "number" : boolean
|
||||
>(b = x) : number | boolean
|
||||
>b = x : number | boolean
|
||||
>b : number | boolean
|
||||
>x : number | boolean
|
||||
|
||||
&& typeof x === "number"
|
||||
>typeof x === "number" : boolean
|
||||
>typeof x : string
|
||||
>x : number | boolean
|
||||
|
||||
&& x.toString()); // x is number
|
||||
>x.toString() : string
|
||||
>x.toString : (radix?: number) => string
|
||||
>x : number
|
||||
>toString : (radix?: number) => string
|
||||
}
|
||||
function foo11(x: number | string | boolean) {
|
||||
>foo11 : (x: string | number | boolean) => string | number | boolean
|
||||
>x : string | number | boolean
|
||||
|
||||
// Mixing typeguards
|
||||
// Assigning value to x deep inside another guard stops narrowing of type too
|
||||
var b: number | boolean | string;
|
||||
>b : string | number | boolean
|
||||
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? x // number | boolean | string - changed in the false branch : ((b = x) // x is number | boolean | string - because the assignment changed it && typeof x === "number" && (x = 10) // assignment to x && x) : string | number | boolean
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number | boolean
|
||||
|
||||
? x // number | boolean | string - changed in the false branch
|
||||
>x : string | number | boolean
|
||||
|
||||
: ((b = x) // x is number | boolean | string - because the assignment changed it
|
||||
>((b = x) // x is number | boolean | string - because the assignment changed it && typeof x === "number" && (x = 10) // assignment to x && x) : string | number | boolean
|
||||
>(b = x) // x is number | boolean | string - because the assignment changed it && typeof x === "number" && (x = 10) // assignment to x && x : string | number | boolean
|
||||
>(b = x) // x is number | boolean | string - because the assignment changed it && typeof x === "number" && (x = 10) : number
|
||||
>(b = x) // x is number | boolean | string - because the assignment changed it && typeof x === "number" : boolean
|
||||
>(b = x) : string | number | boolean
|
||||
>b = x : string | number | boolean
|
||||
>b : string | number | boolean
|
||||
>x : string | number | boolean
|
||||
|
||||
&& typeof x === "number"
|
||||
>typeof x === "number" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number | boolean
|
||||
|
||||
&& (x = 10) // assignment to x
|
||||
>(x = 10) : number
|
||||
>x = 10 : number
|
||||
>x : string | number | boolean
|
||||
|
||||
&& x); // x is number | boolean | string
|
||||
>x : string | number | boolean
|
||||
}
|
||||
function foo12(x: number | string | boolean) {
|
||||
>foo12 : (x: string | number | boolean) => number
|
||||
>x : string | number | boolean
|
||||
|
||||
// Mixing typeguards
|
||||
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
|
||||
var b: number | boolean | string;
|
||||
>b : string | number | boolean
|
||||
|
||||
return typeof x === "string"
|
||||
>typeof x === "string" ? (x = 10 && x.toString().length) // number | boolean | string - changed here : ((b = x) // x is number | boolean | string - changed in true branch && typeof x === "number" && x) : number
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number | boolean
|
||||
|
||||
? (x = 10 && x.toString().length) // number | boolean | string - changed here
|
||||
>(x = 10 && x.toString().length) : number
|
||||
>x = 10 && x.toString().length : number
|
||||
>x : string | number | boolean
|
||||
>10 && x.toString().length : number
|
||||
>x.toString().length : number
|
||||
>x.toString() : string
|
||||
>x.toString : () => string
|
||||
>x : string | number | boolean
|
||||
>toString : () => string
|
||||
>length : number
|
||||
|
||||
: ((b = x) // x is number | boolean | string - changed in true branch
|
||||
>((b = x) // x is number | boolean | string - changed in true branch && typeof x === "number" && x) : number
|
||||
>(b = x) // x is number | boolean | string - changed in true branch && typeof x === "number" && x : number
|
||||
>(b = x) // x is number | boolean | string - changed in true branch && typeof x === "number" : boolean
|
||||
>(b = x) : string | number | boolean
|
||||
>b = x : string | number | boolean
|
||||
>b : string | number | boolean
|
||||
>x : string | number | boolean
|
||||
|
||||
&& typeof x === "number"
|
||||
>typeof x === "number" : boolean
|
||||
>typeof x : string
|
||||
>x : string | number | boolean
|
||||
|
||||
&& x); // x is number
|
||||
>x : number
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
// In the true expression of a conditional expression,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the condition when true,
|
||||
// provided the true expression contains no assignments to the variable or parameter.
|
||||
// In the false expression of a conditional expression,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the condition when false,
|
||||
// provided the false expression contains no assignments to the variable or parameter.
|
||||
|
||||
function foo(x: number | string) {
|
||||
return typeof x === "string"
|
||||
? x.length // string
|
||||
: x++; // number
|
||||
}
|
||||
function foo2(x: number | string) {
|
||||
// x is assigned in the if true branch, the type is not narrowed
|
||||
return typeof x === "string"
|
||||
? (x = 10 && x)// string | number
|
||||
: x; // string | number
|
||||
}
|
||||
function foo3(x: number | string) {
|
||||
// x is assigned in the if false branch, the type is not narrowed
|
||||
// even though assigned using same type as narrowed expression
|
||||
return typeof x === "string"
|
||||
? (x = "Hello" && x) // string | number
|
||||
: x; // string | number
|
||||
}
|
||||
function foo4(x: number | string) {
|
||||
// false branch updates the variable - so here it is not number
|
||||
// even though assigned using same type as narrowed expression
|
||||
return typeof x === "string"
|
||||
? x // string | number
|
||||
: (x = 10 && x); // string | number
|
||||
}
|
||||
function foo5(x: number | string) {
|
||||
// false branch updates the variable - so here it is not number
|
||||
return typeof x === "string"
|
||||
? x // string | number
|
||||
: (x = "hello" && x); // string | number
|
||||
}
|
||||
function foo6(x: number | string) {
|
||||
// Modify in both branches
|
||||
return typeof x === "string"
|
||||
? (x = 10 && x) // string | number
|
||||
: (x = "hello" && x); // string | number
|
||||
}
|
||||
function foo7(x: number | string | boolean) {
|
||||
return typeof x === "string"
|
||||
? x === "hello" // string
|
||||
: typeof x === "boolean"
|
||||
? x // boolean
|
||||
: x == 10; // number
|
||||
}
|
||||
function foo8(x: number | string | boolean) {
|
||||
var b: number | boolean;
|
||||
return typeof x === "string"
|
||||
? x === "hello"
|
||||
: ((b = x) && // number | boolean
|
||||
(typeof x === "boolean"
|
||||
? x // boolean
|
||||
: x == 10)); // number
|
||||
}
|
||||
function foo9(x: number | string) {
|
||||
var y = 10;
|
||||
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
|
||||
return typeof x === "string"
|
||||
? ((y = x.length) && x === "hello") // string
|
||||
: x === 10; // number
|
||||
}
|
||||
function foo10(x: number | string | boolean) {
|
||||
// Mixing typeguards
|
||||
var b: boolean | number;
|
||||
return typeof x === "string"
|
||||
? x // string
|
||||
: ((b = x) // x is number | boolean
|
||||
&& typeof x === "number"
|
||||
&& x.toString()); // x is number
|
||||
}
|
||||
function foo11(x: number | string | boolean) {
|
||||
// Mixing typeguards
|
||||
// Assigning value to x deep inside another guard stops narrowing of type too
|
||||
var b: number | boolean | string;
|
||||
return typeof x === "string"
|
||||
? x // number | boolean | string - changed in the false branch
|
||||
: ((b = x) // x is number | boolean | string - because the assignment changed it
|
||||
&& typeof x === "number"
|
||||
&& (x = 10) // assignment to x
|
||||
&& x); // x is number | boolean | string
|
||||
}
|
||||
function foo12(x: number | string | boolean) {
|
||||
// Mixing typeguards
|
||||
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
|
||||
var b: number | boolean | string;
|
||||
return typeof x === "string"
|
||||
? (x = 10 && x.toString().length) // number | boolean | string - changed here
|
||||
: ((b = x) // x is number | boolean | string - changed in true branch
|
||||
&& typeof x === "number"
|
||||
&& x); // x is number
|
||||
}
|
Loading…
Reference in a new issue