305 lines
11 KiB
Plaintext
305 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 : Symbol(foo, Decl(typeGuardsInIfStatement.ts, 0, 0))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 6, 13))
|
|
|
|
if (typeof x === "string") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 6, 13))
|
|
|
|
return x.length; // string
|
|
>x.length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 6, 13))
|
|
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
|
|
}
|
|
else {
|
|
return x++; // number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 6, 13))
|
|
}
|
|
}
|
|
function foo2(x: number | string) {
|
|
>foo2 : Symbol(foo2, Decl(typeGuardsInIfStatement.ts, 13, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 14, 14))
|
|
|
|
// x is assigned in the if true branch, the type is not narrowed
|
|
if (typeof x === "string") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 14, 14))
|
|
|
|
x = 10;
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 14, 14))
|
|
|
|
return x; // string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 14, 14))
|
|
}
|
|
else {
|
|
return x; // string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 14, 14))
|
|
}
|
|
}
|
|
function foo3(x: number | string) {
|
|
>foo3 : Symbol(foo3, Decl(typeGuardsInIfStatement.ts, 23, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 24, 14))
|
|
|
|
// x is assigned in the if true branch, the type is not narrowed
|
|
if (typeof x === "string") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 24, 14))
|
|
|
|
x = "Hello"; // even though assigned using same type as narrowed expression
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 24, 14))
|
|
|
|
return x; // string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 24, 14))
|
|
}
|
|
else {
|
|
return x; // string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 24, 14))
|
|
}
|
|
}
|
|
function foo4(x: number | string) {
|
|
>foo4 : Symbol(foo4, Decl(typeGuardsInIfStatement.ts, 33, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 34, 14))
|
|
|
|
// false branch updates the variable - so here it is not number
|
|
if (typeof x === "string") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 34, 14))
|
|
|
|
return x; // string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 34, 14))
|
|
}
|
|
else {
|
|
x = 10; // even though assigned number - this should result in x to be string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 34, 14))
|
|
|
|
return x; // string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 34, 14))
|
|
}
|
|
}
|
|
function foo5(x: number | string) {
|
|
>foo5 : Symbol(foo5, Decl(typeGuardsInIfStatement.ts, 43, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 44, 14))
|
|
|
|
// false branch updates the variable - so here it is not number
|
|
if (typeof x === "string") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 44, 14))
|
|
|
|
return x; // string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 44, 14))
|
|
}
|
|
else {
|
|
x = "hello";
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 44, 14))
|
|
|
|
return x; // string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 44, 14))
|
|
}
|
|
}
|
|
function foo6(x: number | string) {
|
|
>foo6 : Symbol(foo6, Decl(typeGuardsInIfStatement.ts, 53, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14))
|
|
|
|
// Modify in both branches
|
|
if (typeof x === "string") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14))
|
|
|
|
x = 10;
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14))
|
|
|
|
return x; // string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14))
|
|
}
|
|
else {
|
|
x = "hello";
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14))
|
|
|
|
return x; // string | number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14))
|
|
}
|
|
}
|
|
function foo7(x: number | string | boolean) {
|
|
>foo7 : Symbol(foo7, Decl(typeGuardsInIfStatement.ts, 64, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14))
|
|
|
|
if (typeof x === "string") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14))
|
|
|
|
return x === "hello"; // string
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14))
|
|
}
|
|
else if (typeof x === "boolean") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14))
|
|
|
|
return x; // boolean
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14))
|
|
}
|
|
else {
|
|
return x == 10; // number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14))
|
|
}
|
|
}
|
|
function foo8(x: number | string | boolean) {
|
|
>foo8 : Symbol(foo8, Decl(typeGuardsInIfStatement.ts, 75, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14))
|
|
|
|
if (typeof x === "string") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14))
|
|
|
|
return x === "hello"; // string
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14))
|
|
}
|
|
else {
|
|
var b: number | boolean = x; // number | boolean
|
|
>b : Symbol(b, Decl(typeGuardsInIfStatement.ts, 81, 11))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14))
|
|
|
|
if (typeof x === "boolean") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14))
|
|
|
|
return x; // boolean
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14))
|
|
}
|
|
else {
|
|
return x == 10; // number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14))
|
|
}
|
|
}
|
|
}
|
|
function foo9(x: number | string) {
|
|
>foo9 : Symbol(foo9, Decl(typeGuardsInIfStatement.ts, 89, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 90, 14))
|
|
|
|
var y = 10;
|
|
>y : Symbol(y, Decl(typeGuardsInIfStatement.ts, 91, 7))
|
|
|
|
if (typeof x === "string") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 90, 14))
|
|
|
|
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
|
|
y = x.length;
|
|
>y : Symbol(y, Decl(typeGuardsInIfStatement.ts, 91, 7))
|
|
>x.length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 90, 14))
|
|
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
|
|
|
|
return x === "hello"; // string
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 90, 14))
|
|
}
|
|
else {
|
|
return x == 10; // number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 90, 14))
|
|
}
|
|
}
|
|
function foo10(x: number | string | boolean) {
|
|
>foo10 : Symbol(foo10, Decl(typeGuardsInIfStatement.ts, 100, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15))
|
|
|
|
// Mixing typeguard narrowing in if statement with conditional expression typeguard
|
|
if (typeof x === "string") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15))
|
|
|
|
return x === "hello"; // string
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15))
|
|
}
|
|
else {
|
|
var y: boolean | string;
|
|
>y : Symbol(y, Decl(typeGuardsInIfStatement.ts, 107, 11))
|
|
|
|
var b = x; // number | boolean
|
|
>b : Symbol(b, Decl(typeGuardsInIfStatement.ts, 108, 11))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15))
|
|
|
|
return typeof x === "number"
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15))
|
|
|
|
? x === 10 // number
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15))
|
|
|
|
: x; // x should be boolean
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15))
|
|
}
|
|
}
|
|
function foo11(x: number | string | boolean) {
|
|
>foo11 : Symbol(foo11, Decl(typeGuardsInIfStatement.ts, 113, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15))
|
|
|
|
// 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") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15))
|
|
|
|
return x; // string | number | boolean - x changed in else branch
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15))
|
|
}
|
|
else {
|
|
var y: number| boolean | string;
|
|
>y : Symbol(y, Decl(typeGuardsInIfStatement.ts, 121, 11))
|
|
|
|
var b = x; // number | boolean | string - because below we are changing value of x in if statement
|
|
>b : Symbol(b, Decl(typeGuardsInIfStatement.ts, 122, 11))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15))
|
|
|
|
return typeof x === "number"
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15))
|
|
|
|
? (
|
|
// change value of x
|
|
x = 10 && x.toString() // number | boolean | string
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15))
|
|
>x.toString : Symbol(toString, Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 96, 26))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15))
|
|
>toString : Symbol(toString, Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 96, 26))
|
|
|
|
)
|
|
: (
|
|
// do not change value
|
|
y = x && x.toString() // number | boolean | string
|
|
>y : Symbol(y, Decl(typeGuardsInIfStatement.ts, 121, 11))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15))
|
|
>x.toString : Symbol(toString, Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 96, 26))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15))
|
|
>toString : Symbol(toString, Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 96, 26))
|
|
|
|
);
|
|
}
|
|
}
|
|
function foo12(x: number | string | boolean) {
|
|
>foo12 : Symbol(foo12, Decl(typeGuardsInIfStatement.ts, 133, 1))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15))
|
|
|
|
// 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") {
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15))
|
|
|
|
return x.toString(); // string | number | boolean - x changed in else branch
|
|
>x.toString : Symbol(toString, Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 96, 26))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15))
|
|
>toString : Symbol(toString, Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 96, 26))
|
|
}
|
|
else {
|
|
x = 10;
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15))
|
|
|
|
var b = x; // number | boolean | string
|
|
>b : Symbol(b, Decl(typeGuardsInIfStatement.ts, 142, 11))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15))
|
|
|
|
return typeof x === "number"
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15))
|
|
|
|
? x.toString() // number
|
|
>x.toString : Symbol(Number.toString, Decl(lib.d.ts, 458, 18))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15))
|
|
>toString : Symbol(Number.toString, Decl(lib.d.ts, 458, 18))
|
|
|
|
: x.toString(); // boolean | string
|
|
>x.toString : Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 96, 26))
|
|
>x : Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15))
|
|
>toString : Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 96, 26))
|
|
}
|
|
}
|