1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
=== tests/cases/conformance/expressions/typeGuards/typeGuardsDefeat.ts ===
// Also note that it is possible to defeat a type guard by calling a function that changes the
// type of the guarded variable.
function foo(x: number | string) {
>foo : Symbol(foo, Decl(typeGuardsDefeat.ts, 0, 0))
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 2, 13))
function f() {
>f : Symbol(f, Decl(typeGuardsDefeat.ts, 2, 34))
x = 10;
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 2, 13))
}
if (typeof x === "string") {
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 2, 13))
f();
>f : Symbol(f, Decl(typeGuardsDefeat.ts, 2, 34))
return x.length; // string
>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 2, 13))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
}
else {
return x++; // number
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 2, 13))
}
}
function foo2(x: number | string) {
>foo2 : Symbol(foo2, Decl(typeGuardsDefeat.ts, 13, 1))
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 14, 14))
if (typeof x === "string") {
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 14, 14))
return x.length; // string
>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 14, 14))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
}
else {
var f = function () {
>f : Symbol(f, Decl(typeGuardsDefeat.ts, 19, 11))
return x * x;
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 14, 14))
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 14, 14))
};
}
x = "hello";
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 14, 14))
f();
>f : Symbol(f, Decl(typeGuardsDefeat.ts, 19, 11))
}
function foo3(x: number | string) {
>foo3 : Symbol(foo3, Decl(typeGuardsDefeat.ts, 25, 1))
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 26, 14))
if (typeof x === "string") {
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 26, 14))
return x.length; // string
>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 26, 14))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
}
else {
var f = () => x * x;
>f : Symbol(f, Decl(typeGuardsDefeat.ts, 31, 11))
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 26, 14))
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 26, 14))
}
x = "hello";
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 26, 14))
f();
>f : Symbol(f, Decl(typeGuardsDefeat.ts, 31, 11))
}
|