406 lines
11 KiB
Plaintext
406 lines
11 KiB
Plaintext
=== tests/cases/conformance/expressions/typeGuards/typeGuardsInIfStatement.ts ===
|
|
// In the true branch statement of an 'if' statement,
|
|
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when true,
|
|
// provided the true branch statement contains no assignments to the variable or parameter.
|
|
// In the false branch statement of an 'if' statement,
|
|
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when false,
|
|
// provided the false branch statement contains no assignments to the variable or parameter
|
|
function foo(x: number | string) {
|
|
>foo : (x: number | string) => number
|
|
>x : number | string
|
|
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string
|
|
>"string" : string
|
|
|
|
return x.length; // string
|
|
>x.length : number
|
|
>x : string
|
|
>length : number
|
|
}
|
|
else {
|
|
return x++; // number
|
|
>x++ : number
|
|
>x : number
|
|
}
|
|
}
|
|
function foo2(x: number | string) {
|
|
>foo2 : (x: number | string) => number | string
|
|
>x : number | string
|
|
|
|
// x is assigned in the if true branch, the type is not narrowed
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string
|
|
>"string" : string
|
|
|
|
x = 10;
|
|
>x = 10 : number
|
|
>x : number | string
|
|
>10 : number
|
|
|
|
return x; // string | number
|
|
>x : number | string
|
|
}
|
|
else {
|
|
return x; // string | number
|
|
>x : number | string
|
|
}
|
|
}
|
|
function foo3(x: number | string) {
|
|
>foo3 : (x: number | string) => number | string
|
|
>x : number | string
|
|
|
|
// x is assigned in the if true branch, the type is not narrowed
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string
|
|
>"string" : string
|
|
|
|
x = "Hello"; // even though assigned using same type as narrowed expression
|
|
>x = "Hello" : string
|
|
>x : number | string
|
|
>"Hello" : string
|
|
|
|
return x; // string | number
|
|
>x : number | string
|
|
}
|
|
else {
|
|
return x; // string | number
|
|
>x : number | string
|
|
}
|
|
}
|
|
function foo4(x: number | string) {
|
|
>foo4 : (x: number | string) => number | string
|
|
>x : number | string
|
|
|
|
// false branch updates the variable - so here it is not number
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string
|
|
>"string" : string
|
|
|
|
return x; // string | number
|
|
>x : number | string
|
|
}
|
|
else {
|
|
x = 10; // even though assigned number - this should result in x to be string | number
|
|
>x = 10 : number
|
|
>x : number | string
|
|
>10 : number
|
|
|
|
return x; // string | number
|
|
>x : number | string
|
|
}
|
|
}
|
|
function foo5(x: number | string) {
|
|
>foo5 : (x: number | string) => number | string
|
|
>x : number | string
|
|
|
|
// false branch updates the variable - so here it is not number
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string
|
|
>"string" : string
|
|
|
|
return x; // string | number
|
|
>x : number | string
|
|
}
|
|
else {
|
|
x = "hello";
|
|
>x = "hello" : string
|
|
>x : number | string
|
|
>"hello" : string
|
|
|
|
return x; // string | number
|
|
>x : number | string
|
|
}
|
|
}
|
|
function foo6(x: number | string) {
|
|
>foo6 : (x: number | string) => number | string
|
|
>x : number | string
|
|
|
|
// Modify in both branches
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string
|
|
>"string" : string
|
|
|
|
x = 10;
|
|
>x = 10 : number
|
|
>x : number | string
|
|
>10 : number
|
|
|
|
return x; // string | number
|
|
>x : number | string
|
|
}
|
|
else {
|
|
x = "hello";
|
|
>x = "hello" : string
|
|
>x : number | string
|
|
>"hello" : string
|
|
|
|
return x; // string | number
|
|
>x : number | string
|
|
}
|
|
}
|
|
function foo7(x: number | string | boolean) {
|
|
>foo7 : (x: number | string | boolean) => boolean
|
|
>x : number | string | boolean
|
|
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string | boolean
|
|
>"string" : string
|
|
|
|
return x === "hello"; // string
|
|
>x === "hello" : boolean
|
|
>x : string
|
|
>"hello" : string
|
|
}
|
|
else if (typeof x === "boolean") {
|
|
>typeof x === "boolean" : boolean
|
|
>typeof x : string
|
|
>x : number | boolean
|
|
>"boolean" : string
|
|
|
|
return x; // boolean
|
|
>x : boolean
|
|
}
|
|
else {
|
|
return x == 10; // number
|
|
>x == 10 : boolean
|
|
>x : number
|
|
>10 : number
|
|
}
|
|
}
|
|
function foo8(x: number | string | boolean) {
|
|
>foo8 : (x: number | string | boolean) => boolean
|
|
>x : number | string | boolean
|
|
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string | boolean
|
|
>"string" : string
|
|
|
|
return x === "hello"; // string
|
|
>x === "hello" : boolean
|
|
>x : string
|
|
>"hello" : string
|
|
}
|
|
else {
|
|
var b: number | boolean = x; // number | boolean
|
|
>b : number | boolean
|
|
>x : number | boolean
|
|
|
|
if (typeof x === "boolean") {
|
|
>typeof x === "boolean" : boolean
|
|
>typeof x : string
|
|
>x : number | boolean
|
|
>"boolean" : string
|
|
|
|
return x; // boolean
|
|
>x : boolean
|
|
}
|
|
else {
|
|
return x == 10; // number
|
|
>x == 10 : boolean
|
|
>x : number
|
|
>10 : number
|
|
}
|
|
}
|
|
}
|
|
function foo9(x: number | string) {
|
|
>foo9 : (x: number | string) => boolean
|
|
>x : number | string
|
|
|
|
var y = 10;
|
|
>y : number
|
|
>10 : number
|
|
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string
|
|
>"string" : string
|
|
|
|
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
|
|
y = x.length;
|
|
>y = x.length : number
|
|
>y : number
|
|
>x.length : number
|
|
>x : string
|
|
>length : number
|
|
|
|
return x === "hello"; // string
|
|
>x === "hello" : boolean
|
|
>x : string
|
|
>"hello" : string
|
|
}
|
|
else {
|
|
return x == 10; // number
|
|
>x == 10 : boolean
|
|
>x : number
|
|
>10 : number
|
|
}
|
|
}
|
|
function foo10(x: number | string | boolean) {
|
|
>foo10 : (x: number | string | boolean) => boolean
|
|
>x : number | string | boolean
|
|
|
|
// Mixing typeguard narrowing in if statement with conditional expression typeguard
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string | boolean
|
|
>"string" : string
|
|
|
|
return x === "hello"; // string
|
|
>x === "hello" : boolean
|
|
>x : string
|
|
>"hello" : string
|
|
}
|
|
else {
|
|
var y: boolean | string;
|
|
>y : boolean | string
|
|
|
|
var b = x; // number | boolean
|
|
>b : number | boolean
|
|
>x : number | boolean
|
|
|
|
return typeof x === "number"
|
|
>typeof x === "number" ? x === 10 // number : x : boolean
|
|
>typeof x === "number" : boolean
|
|
>typeof x : string
|
|
>x : number | boolean
|
|
>"number" : string
|
|
|
|
? x === 10 // number
|
|
>x === 10 : boolean
|
|
>x : number
|
|
>10 : number
|
|
|
|
: x; // x should be boolean
|
|
>x : boolean
|
|
}
|
|
}
|
|
function foo11(x: number | string | boolean) {
|
|
>foo11 : (x: number | string | boolean) => number | string | boolean
|
|
>x : number | string | boolean
|
|
|
|
// Mixing typeguard narrowing in if statement with conditional expression typeguard
|
|
// Assigning value to x deep inside another guard stops narrowing of type too
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string | boolean
|
|
>"string" : string
|
|
|
|
return x; // string | number | boolean - x changed in else branch
|
|
>x : number | string | boolean
|
|
}
|
|
else {
|
|
var y: number| boolean | string;
|
|
>y : number | boolean | string
|
|
|
|
var b = x; // number | boolean | string - because below we are changing value of x in if statement
|
|
>b : number | string | boolean
|
|
>x : number | string | boolean
|
|
|
|
return typeof x === "number"
|
|
>typeof x === "number" ? ( // change value of x x = 10 && x.toString() // number | boolean | string ) : ( // do not change value y = x && x.toString() // number | boolean | string ) : string
|
|
>typeof x === "number" : boolean
|
|
>typeof x : string
|
|
>x : number | string | boolean
|
|
>"number" : string
|
|
|
|
? (
|
|
>( // change value of x x = 10 && x.toString() // number | boolean | string ) : string
|
|
|
|
// change value of x
|
|
x = 10 && x.toString() // number | boolean | string
|
|
>x = 10 && x.toString() : string
|
|
>x : number | string | boolean
|
|
>10 && x.toString() : string
|
|
>10 : number
|
|
>x.toString() : string
|
|
>x.toString : (radix?: number) => string
|
|
>x : number | string | boolean
|
|
>toString : (radix?: number) => string
|
|
|
|
)
|
|
: (
|
|
>( // do not change value y = x && x.toString() // number | boolean | string ) : string
|
|
|
|
// do not change value
|
|
y = x && x.toString() // number | boolean | string
|
|
>y = x && x.toString() : string
|
|
>y : number | boolean | string
|
|
>x && x.toString() : string
|
|
>x : number | string | boolean
|
|
>x.toString() : string
|
|
>x.toString : (radix?: number) => string
|
|
>x : number | string | boolean
|
|
>toString : (radix?: number) => string
|
|
|
|
);
|
|
}
|
|
}
|
|
function foo12(x: number | string | boolean) {
|
|
>foo12 : (x: number | string | boolean) => string
|
|
>x : number | string | boolean
|
|
|
|
// Mixing typeguard narrowing in if statement with conditional expression typeguard
|
|
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
|
|
if (typeof x === "string") {
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : number | string | boolean
|
|
>"string" : string
|
|
|
|
return x.toString(); // string | number | boolean - x changed in else branch
|
|
>x.toString() : string
|
|
>x.toString : (radix?: number) => string
|
|
>x : number | string | boolean
|
|
>toString : (radix?: number) => string
|
|
}
|
|
else {
|
|
x = 10;
|
|
>x = 10 : number
|
|
>x : number | string | boolean
|
|
>10 : number
|
|
|
|
var b = x; // number | boolean | string
|
|
>b : number | string | boolean
|
|
>x : number | string | boolean
|
|
|
|
return typeof x === "number"
|
|
>typeof x === "number" ? x.toString() // number : x.toString() : string
|
|
>typeof x === "number" : boolean
|
|
>typeof x : string
|
|
>x : number | string | boolean
|
|
>"number" : string
|
|
|
|
? x.toString() // number
|
|
>x.toString() : string
|
|
>x.toString : (radix?: number) => string
|
|
>x : number
|
|
>toString : (radix?: number) => string
|
|
|
|
: x.toString(); // boolean | string
|
|
>x.toString() : string
|
|
>x.toString : () => string
|
|
>x : string | boolean
|
|
>toString : () => string
|
|
}
|
|
}
|