388 lines
13 KiB
Text
388 lines
13 KiB
Text
=== 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
|
|
>"string" : string
|
|
|
|
? 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
|
|
>"string" : string
|
|
|
|
? (x = 10 && x)// string | number
|
|
>(x = 10 && x) : string | number
|
|
>x = 10 && x : string | number
|
|
>x : string | number
|
|
>10 && x : string | number
|
|
>10 : 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
|
|
>"string" : string
|
|
|
|
? (x = "Hello" && x) // string | number
|
|
>(x = "Hello" && x) : string | number
|
|
>x = "Hello" && x : string | number
|
|
>x : string | number
|
|
>"Hello" && x : string | number
|
|
>"Hello" : string
|
|
>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
|
|
>"string" : string
|
|
|
|
? 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
|
|
>10 : 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
|
|
>"string" : string
|
|
|
|
? 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
|
|
>"hello" : string
|
|
>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
|
|
>"string" : string
|
|
|
|
? (x = 10 && x) // string | number
|
|
>(x = 10 && x) : string | number
|
|
>x = 10 && x : string | number
|
|
>x : string | number
|
|
>10 && x : string | number
|
|
>10 : 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
|
|
>"hello" : string
|
|
>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
|
|
>"string" : string
|
|
|
|
? x === "hello" // string
|
|
>x === "hello" : boolean
|
|
>x : string
|
|
>"hello" : string
|
|
|
|
: typeof x === "boolean"
|
|
>typeof x === "boolean" ? x // boolean : x == 10 : boolean
|
|
>typeof x === "boolean" : boolean
|
|
>typeof x : string
|
|
>x : number | boolean
|
|
>"boolean" : string
|
|
|
|
? x // boolean
|
|
>x : boolean
|
|
|
|
: x == 10; // number
|
|
>x == 10 : boolean
|
|
>x : number
|
|
>10 : 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
|
|
>"string" : string
|
|
|
|
? x === "hello"
|
|
>x === "hello" : boolean
|
|
>x : string
|
|
>"hello" : 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
|
|
>"boolean" : string
|
|
|
|
? x // boolean
|
|
>x : boolean
|
|
|
|
: x == 10)); // number
|
|
>x == 10 : boolean
|
|
>x : number
|
|
>10 : number
|
|
}
|
|
function foo9(x: number | string) {
|
|
>foo9 : (x: string | number) => boolean
|
|
>x : string | number
|
|
|
|
var y = 10;
|
|
>y : number
|
|
>10 : 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
|
|
>"string" : string
|
|
|
|
? ((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
|
|
>"hello" : string
|
|
|
|
: x === 10; // number
|
|
>x === 10 : boolean
|
|
>x : number
|
|
>10 : 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
|
|
>"string" : string
|
|
|
|
? 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
|
|
>"number" : string
|
|
|
|
&& 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
|
|
>"string" : string
|
|
|
|
? 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
|
|
>"number" : string
|
|
|
|
&& (x = 10) // assignment to x
|
|
>(x = 10) : number
|
|
>x = 10 : number
|
|
>x : string | number | boolean
|
|
>10 : number
|
|
|
|
&& 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
|
|
>"string" : string
|
|
|
|
? (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
|
|
>10 : number
|
|
>x.toString().length : number
|
|
>x.toString() : string
|
|
>x.toString : (radix?: number) => string
|
|
>x : string | number | boolean
|
|
>toString : (radix?: number) => 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
|
|
>"number" : string
|
|
|
|
&& x); // x is number
|
|
>x : number
|
|
}
|