From 2088a89223f246191a31ee3e6b5798e06a51881b Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 5 Nov 2014 14:12:29 -0800 Subject: [PATCH] Test cases to make sure typeguard is defeated in case of function calls From spec: Also note that it is possible to defeat a type guard by calling a function that changes the type of the guarded variable. --- tests/baselines/reference/typeGuardsDefeat.js | 75 +++++++++++++++ .../reference/typeGuardsDefeat.types | 95 +++++++++++++++++++ .../typeGuards/typeGuardsDefeat.ts | 36 +++++++ 3 files changed, 206 insertions(+) create mode 100644 tests/baselines/reference/typeGuardsDefeat.js create mode 100644 tests/baselines/reference/typeGuardsDefeat.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardsDefeat.ts diff --git a/tests/baselines/reference/typeGuardsDefeat.js b/tests/baselines/reference/typeGuardsDefeat.js new file mode 100644 index 0000000000..b5e1e0c362 --- /dev/null +++ b/tests/baselines/reference/typeGuardsDefeat.js @@ -0,0 +1,75 @@ +//// [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) { + function f() { + x = 10; + } + if (typeof x === "string") { + f(); + return x.length; // string + } + else { + return x++; // number + } +} +function foo2(x: number | string) { + if (typeof x === "string") { + return x.length; // string + } + else { + (function f() { + x = 10; + })(); + return x++; // number + } +} +function foo3(x: number | string) { + if (typeof x === "string") { + return x.length; // string + } + else { + (() => { + x = 10; + })(); + return x++; // number + } +} + +//// [typeGuardsDefeat.js] +// 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) { + function f() { + x = 10; + } + if (typeof x === "string") { + f(); + return x.length; // string + } + else { + return x++; // number + } +} +function foo2(x) { + if (typeof x === "string") { + return x.length; // string + } + else { + (function f() { + x = 10; + })(); + return x++; // number + } +} +function foo3(x) { + if (typeof x === "string") { + return x.length; // string + } + else { + (function () { + x = 10; + })(); + return x++; // number + } +} diff --git a/tests/baselines/reference/typeGuardsDefeat.types b/tests/baselines/reference/typeGuardsDefeat.types new file mode 100644 index 0000000000..6c4153cc3e --- /dev/null +++ b/tests/baselines/reference/typeGuardsDefeat.types @@ -0,0 +1,95 @@ +=== 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 : (x: string | number) => number +>x : string | number + + function f() { +>f : () => void + + x = 10; +>x = 10 : number +>x : string | number + } + if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : string +>x : string | number + + f(); +>f() : void +>f : () => void + + 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: string | number) => number +>x : string | number + + if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : string +>x : string | number + + return x.length; // string +>x.length : number +>x : string +>length : number + } + else { + (function f() { +>(function f() { x = 10; })() : void +>(function f() { x = 10; }) : () => void +>function f() { x = 10; } : () => void +>f : () => void + + x = 10; +>x = 10 : number +>x : string | number + + })(); + return x++; // number +>x++ : number +>x : number + } +} +function foo3(x: number | string) { +>foo3 : (x: string | number) => number +>x : string | number + + if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : string +>x : string | number + + return x.length; // string +>x.length : number +>x : string +>length : number + } + else { + (() => { +>(() => { x = 10; })() : void +>(() => { x = 10; }) : () => void +>() => { x = 10; } : () => void + + x = 10; +>x = 10 : number +>x : string | number + + })(); + return x++; // number +>x++ : number +>x : number + } +} diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardsDefeat.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardsDefeat.ts new file mode 100644 index 0000000000..9c78e72539 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardsDefeat.ts @@ -0,0 +1,36 @@ +// 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) { + function f() { + x = 10; + } + if (typeof x === "string") { + f(); + return x.length; // string + } + else { + return x++; // number + } +} +function foo2(x: number | string) { + if (typeof x === "string") { + return x.length; // string + } + else { + (function f() { + x = 10; + })(); + return x++; // number + } +} +function foo3(x: number | string) { + if (typeof x === "string") { + return x.length; // string + } + else { + (() => { + x = 10; + })(); + return x++; // number + } +} \ No newline at end of file