82 lines
2.5 KiB
Text
82 lines
2.5 KiB
Text
=== 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.d.ts, 414, 19))
|
|
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 2, 13))
|
|
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
|
|
}
|
|
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.d.ts, 414, 19))
|
|
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 14, 14))
|
|
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
|
|
}
|
|
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.d.ts, 414, 19))
|
|
>x : Symbol(x, Decl(typeGuardsDefeat.ts, 26, 14))
|
|
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
|
|
}
|
|
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))
|
|
}
|
|
|