From 7ebf5371a5de0c9ff5419cb7d78a182bd120e324 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 5 Nov 2014 18:31:38 -0800 Subject: [PATCH] =?UTF-8?q?Test=20cases=20for=20typeguards=20of=20form=20t?= =?UTF-8?q?ypeof=20x=20=3D=3D=3D=20s=20and=20typeof=20x=20!=3D=3D=20s=20?= =?UTF-8?q?=E2=80=A2=09A=20type=20guard=20of=20the=20form=20typeof=20x=20?= =?UTF-8?q?=3D=3D=3D=20s,=20where=20s=20is=20a=20string=20literal=20with?= =?UTF-8?q?=20the=20value=20=E2=80=98string=E2=80=99,=20=E2=80=98number?= =?UTF-8?q?=E2=80=99,=20or=20=E2=80=98boolean=E2=80=99,=20o=09when=20true,?= =?UTF-8?q?=20narrows=20the=20type=20of=20x=20to=20the=20given=20primitive?= =?UTF-8?q?=20type,=20or=20o=09when=20false,=20removes=20the=20primitive?= =?UTF-8?q?=20type=20from=20the=20type=20of=20x.=20=E2=80=A2=09A=20type=20?= =?UTF-8?q?guard=20of=20the=20form=20typeof=20x=20=3D=3D=3D=20s,=20where?= =?UTF-8?q?=20s=20is=20a=20string=20literal=20with=20any=20value=20but=20?= =?UTF-8?q?=E2=80=98string=E2=80=99,=20=E2=80=98number=E2=80=99,=20or=20?= =?UTF-8?q?=E2=80=98boolean=E2=80=99,=20o=09when=20true,=20removes=20the?= =?UTF-8?q?=20primitive=20types=20string,=20number,=20and=20boolean=20from?= =?UTF-8?q?=20the=20type=20of=20x,=20or=20o=09when=20false,=20has=20no=20e?= =?UTF-8?q?ffect=20on=20the=20type=20of=20x.=20=E2=80=A2=09A=20type=20guar?= =?UTF-8?q?d=20of=20the=20form=20typeof=20x=20!=3D=3D=20s,=20where=20s=20i?= =?UTF-8?q?s=20a=20string=20literal,=20o=09when=20true,=20narrows=20the=20?= =?UTF-8?q?type=20of=20x=20by=20typeof=20x=20=3D=3D=3D=20s=20when=20false,?= =?UTF-8?q?=20or=20o=09when=20false,=20narrows=20the=20type=20of=20x=20by?= =?UTF-8?q?=20typeof=20x=20=3D=3D=3D=20s=20when=20true.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reference/typeGuardOfFormTypeOfBoolean.js | 169 ++++++++++++++ .../typeGuardOfFormTypeOfBoolean.types | 208 ++++++++++++++++++ .../reference/typeGuardOfFormTypeOfNumber.js | 169 ++++++++++++++ .../typeGuardOfFormTypeOfNumber.types | 206 +++++++++++++++++ .../reference/typeGuardOfFormTypeOfOther.js | 148 +++++++++++++ .../typeGuardOfFormTypeOfOther.types | 180 +++++++++++++++ .../reference/typeGuardOfFormTypeOfString.js | 169 ++++++++++++++ .../typeGuardOfFormTypeOfString.types | 208 ++++++++++++++++++ .../typeGuardOfFormTypeOfBoolean.ts | 82 +++++++ .../typeGuards/typeGuardOfFormTypeOfNumber.ts | 82 +++++++ .../typeGuards/typeGuardOfFormTypeOfOther.ts | 72 ++++++ .../typeGuards/typeGuardOfFormTypeOfString.ts | 82 +++++++ 12 files changed, 1775 insertions(+) create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfBoolean.js create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfBoolean.types create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfNumber.js create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfNumber.types create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfOther.js create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfOther.types create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfString.js create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfString.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfBoolean.ts create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfNumber.ts create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfString.ts diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfBoolean.js b/tests/baselines/reference/typeGuardOfFormTypeOfBoolean.js new file mode 100644 index 00000000000..4b675ffd65a --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfBoolean.js @@ -0,0 +1,169 @@ +//// [typeGuardOfFormTypeOfBoolean.ts] +class C { private p: string }; + +var str: string; +var bool: boolean; +var num: number; +var strOrNum: string | number; +var strOrBool: string | boolean; +var numOrBool: number | boolean +var strOrNumOrBool: string | number | boolean; +var strOrC: string | C; +var numOrC: number | C; +var boolOrC: boolean | C; +var c: C; + +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "boolean") { + bool = strOrNum; // boolean +} +else { + var z: string | number = strOrNum; // string | number +} +if (typeof strOrBool === "boolean") { + bool = strOrBool; // boolean +} +else { + str = strOrBool; // string +} +if (typeof numOrBool === "boolean") { + bool = numOrBool; // boolean +} +else { + num = numOrBool; // number +} +if (typeof strOrNumOrBool === "boolean") { + bool = strOrNumOrBool; // boolean +} +else { + strOrNum = strOrNumOrBool; // string | number +} +if (typeof boolOrC === "boolean") { + bool = boolOrC; // boolean +} +else { + c = boolOrC; // C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "boolean") { + var z: string | number = strOrNum; // string | number +} +else { + bool = strOrNum; // boolean +} +if (typeof strOrBool !== "boolean") { + str = strOrBool; // string +} +else { + bool = strOrBool; // boolean +} +if (typeof numOrBool !== "boolean") { + num = numOrBool; // number +} +else { + bool = numOrBool; // boolean +} +if (typeof strOrNumOrBool !== "boolean") { + strOrNum = strOrNumOrBool; // string | number +} +else { + bool = strOrNumOrBool; // boolean +} +if (typeof boolOrC !== "boolean") { + c = boolOrC; // C +} +else { + bool = boolOrC; // boolean +} + +//// [typeGuardOfFormTypeOfBoolean.js] +var C = (function () { + function C() { + } + return C; +})(); +; +var str; +var bool; +var num; +var strOrNum; +var strOrBool; +var numOrBool; +var strOrNumOrBool; +var strOrC; +var numOrC; +var boolOrC; +var c; +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "boolean") { + bool = strOrNum; // boolean +} +else { + var z = strOrNum; // string | number +} +if (typeof strOrBool === "boolean") { + bool = strOrBool; // boolean +} +else { + str = strOrBool; // string +} +if (typeof numOrBool === "boolean") { + bool = numOrBool; // boolean +} +else { + num = numOrBool; // number +} +if (typeof strOrNumOrBool === "boolean") { + bool = strOrNumOrBool; // boolean +} +else { + strOrNum = strOrNumOrBool; // string | number +} +if (typeof boolOrC === "boolean") { + bool = boolOrC; // boolean +} +else { + c = boolOrC; // C +} +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "boolean") { + var z = strOrNum; // string | number +} +else { + bool = strOrNum; // boolean +} +if (typeof strOrBool !== "boolean") { + str = strOrBool; // string +} +else { + bool = strOrBool; // boolean +} +if (typeof numOrBool !== "boolean") { + num = numOrBool; // number +} +else { + bool = numOrBool; // boolean +} +if (typeof strOrNumOrBool !== "boolean") { + strOrNum = strOrNumOrBool; // string | number +} +else { + bool = strOrNumOrBool; // boolean +} +if (typeof boolOrC !== "boolean") { + c = boolOrC; // C +} +else { + bool = boolOrC; // boolean +} diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfBoolean.types b/tests/baselines/reference/typeGuardOfFormTypeOfBoolean.types new file mode 100644 index 00000000000..6dc9792768c --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfBoolean.types @@ -0,0 +1,208 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfBoolean.ts === +class C { private p: string }; +>C : C +>p : string + +var str: string; +>str : string + +var bool: boolean; +>bool : boolean + +var num: number; +>num : number + +var strOrNum: string | number; +>strOrNum : string | number + +var strOrBool: string | boolean; +>strOrBool : string | boolean + +var numOrBool: number | boolean +>numOrBool : number | boolean + +var strOrNumOrBool: string | number | boolean; +>strOrNumOrBool : string | number | boolean + +var strOrC: string | C; +>strOrC : string | C +>C : C + +var numOrC: number | C; +>numOrC : number | C +>C : C + +var boolOrC: boolean | C; +>boolOrC : boolean | C +>C : C + +var c: C; +>c : C +>C : C + +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "boolean") { +>typeof strOrNum === "boolean" : boolean +>typeof strOrNum : string +>strOrNum : string | number + + bool = strOrNum; // boolean +>bool = strOrNum : boolean +>bool : boolean +>strOrNum : boolean +} +else { + var z: string | number = strOrNum; // string | number +>z : string | number +>strOrNum : string | number +} +if (typeof strOrBool === "boolean") { +>typeof strOrBool === "boolean" : boolean +>typeof strOrBool : string +>strOrBool : string | boolean + + bool = strOrBool; // boolean +>bool = strOrBool : boolean +>bool : boolean +>strOrBool : boolean +} +else { + str = strOrBool; // string +>str = strOrBool : string +>str : string +>strOrBool : string +} +if (typeof numOrBool === "boolean") { +>typeof numOrBool === "boolean" : boolean +>typeof numOrBool : string +>numOrBool : number | boolean + + bool = numOrBool; // boolean +>bool = numOrBool : boolean +>bool : boolean +>numOrBool : boolean +} +else { + num = numOrBool; // number +>num = numOrBool : number +>num : number +>numOrBool : number +} +if (typeof strOrNumOrBool === "boolean") { +>typeof strOrNumOrBool === "boolean" : boolean +>typeof strOrNumOrBool : string +>strOrNumOrBool : string | number | boolean + + bool = strOrNumOrBool; // boolean +>bool = strOrNumOrBool : boolean +>bool : boolean +>strOrNumOrBool : boolean +} +else { + strOrNum = strOrNumOrBool; // string | number +>strOrNum = strOrNumOrBool : string | number +>strOrNum : string | number +>strOrNumOrBool : string | number +} +if (typeof boolOrC === "boolean") { +>typeof boolOrC === "boolean" : boolean +>typeof boolOrC : string +>boolOrC : boolean | C + + bool = boolOrC; // boolean +>bool = boolOrC : boolean +>bool : boolean +>boolOrC : boolean +} +else { + c = boolOrC; // C +>c = boolOrC : C +>c : C +>boolOrC : C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "boolean") { +>typeof strOrNum !== "boolean" : boolean +>typeof strOrNum : string +>strOrNum : string | number + + var z: string | number = strOrNum; // string | number +>z : string | number +>strOrNum : string | number +} +else { + bool = strOrNum; // boolean +>bool = strOrNum : boolean +>bool : boolean +>strOrNum : boolean +} +if (typeof strOrBool !== "boolean") { +>typeof strOrBool !== "boolean" : boolean +>typeof strOrBool : string +>strOrBool : string | boolean + + str = strOrBool; // string +>str = strOrBool : string +>str : string +>strOrBool : string +} +else { + bool = strOrBool; // boolean +>bool = strOrBool : boolean +>bool : boolean +>strOrBool : boolean +} +if (typeof numOrBool !== "boolean") { +>typeof numOrBool !== "boolean" : boolean +>typeof numOrBool : string +>numOrBool : number | boolean + + num = numOrBool; // number +>num = numOrBool : number +>num : number +>numOrBool : number +} +else { + bool = numOrBool; // boolean +>bool = numOrBool : boolean +>bool : boolean +>numOrBool : boolean +} +if (typeof strOrNumOrBool !== "boolean") { +>typeof strOrNumOrBool !== "boolean" : boolean +>typeof strOrNumOrBool : string +>strOrNumOrBool : string | number | boolean + + strOrNum = strOrNumOrBool; // string | number +>strOrNum = strOrNumOrBool : string | number +>strOrNum : string | number +>strOrNumOrBool : string | number +} +else { + bool = strOrNumOrBool; // boolean +>bool = strOrNumOrBool : boolean +>bool : boolean +>strOrNumOrBool : boolean +} +if (typeof boolOrC !== "boolean") { +>typeof boolOrC !== "boolean" : boolean +>typeof boolOrC : string +>boolOrC : boolean | C + + c = boolOrC; // C +>c = boolOrC : C +>c : C +>boolOrC : C +} +else { + bool = boolOrC; // boolean +>bool = boolOrC : boolean +>bool : boolean +>boolOrC : boolean +} diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfNumber.js b/tests/baselines/reference/typeGuardOfFormTypeOfNumber.js new file mode 100644 index 00000000000..ff3a43430ba --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfNumber.js @@ -0,0 +1,169 @@ +//// [typeGuardOfFormTypeOfNumber.ts] +class C { private p: string }; + +var str: string; +var bool: boolean; +var num: number; +var strOrNum: string | number; +var strOrBool: string | boolean; +var numOrBool: number | boolean +var strOrNumOrBool: string | number | boolean; +var strOrC: string | C; +var numOrC: number | C; +var boolOrC: boolean | C; +var c: C; + +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "number") { + num = strOrNum; // number +} +else { + str === strOrNum; // string +} +if (typeof strOrBool === "number") { + num = strOrBool; // number +} +else { + var y: string | boolean = strOrBool; // string | boolean +} +if (typeof numOrBool === "number") { + num = numOrBool; // number +} +else { + var x: number | boolean = numOrBool; // number | boolean +} +if (typeof strOrNumOrBool === "number") { + num = strOrNumOrBool; // number +} +else { + strOrBool = strOrNumOrBool; // string | boolean +} +if (typeof numOrC === "number") { + num = numOrC; // number +} +else { + c = numOrC; // C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "number") { + str === strOrNum; // string +} +else { + num = strOrNum; // number +} +if (typeof strOrBool !== "number") { + var y: string | boolean = strOrBool; // string | boolean +} +else { + num = strOrBool; // number +} +if (typeof numOrBool !== "number") { + var x: number | boolean = numOrBool; // number | boolean +} +else { + num = numOrBool; // number +} +if (typeof strOrNumOrBool !== "number") { + strOrBool = strOrNumOrBool; // string | boolean +} +else { + num = strOrNumOrBool; // number +} +if (typeof numOrC !== "number") { + c = numOrC; // C +} +else { + num = numOrC; // number +} + +//// [typeGuardOfFormTypeOfNumber.js] +var C = (function () { + function C() { + } + return C; +})(); +; +var str; +var bool; +var num; +var strOrNum; +var strOrBool; +var numOrBool; +var strOrNumOrBool; +var strOrC; +var numOrC; +var boolOrC; +var c; +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "number") { + num = strOrNum; // number +} +else { + str === strOrNum; // string +} +if (typeof strOrBool === "number") { + num = strOrBool; // number +} +else { + var y = strOrBool; // string | boolean +} +if (typeof numOrBool === "number") { + num = numOrBool; // number +} +else { + var x = numOrBool; // number | boolean +} +if (typeof strOrNumOrBool === "number") { + num = strOrNumOrBool; // number +} +else { + strOrBool = strOrNumOrBool; // string | boolean +} +if (typeof numOrC === "number") { + num = numOrC; // number +} +else { + c = numOrC; // C +} +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "number") { + str === strOrNum; // string +} +else { + num = strOrNum; // number +} +if (typeof strOrBool !== "number") { + var y = strOrBool; // string | boolean +} +else { + num = strOrBool; // number +} +if (typeof numOrBool !== "number") { + var x = numOrBool; // number | boolean +} +else { + num = numOrBool; // number +} +if (typeof strOrNumOrBool !== "number") { + strOrBool = strOrNumOrBool; // string | boolean +} +else { + num = strOrNumOrBool; // number +} +if (typeof numOrC !== "number") { + c = numOrC; // C +} +else { + num = numOrC; // number +} diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfNumber.types b/tests/baselines/reference/typeGuardOfFormTypeOfNumber.types new file mode 100644 index 00000000000..7fd8855ec92 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfNumber.types @@ -0,0 +1,206 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfNumber.ts === +class C { private p: string }; +>C : C +>p : string + +var str: string; +>str : string + +var bool: boolean; +>bool : boolean + +var num: number; +>num : number + +var strOrNum: string | number; +>strOrNum : string | number + +var strOrBool: string | boolean; +>strOrBool : string | boolean + +var numOrBool: number | boolean +>numOrBool : number | boolean + +var strOrNumOrBool: string | number | boolean; +>strOrNumOrBool : string | number | boolean + +var strOrC: string | C; +>strOrC : string | C +>C : C + +var numOrC: number | C; +>numOrC : number | C +>C : C + +var boolOrC: boolean | C; +>boolOrC : boolean | C +>C : C + +var c: C; +>c : C +>C : C + +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "number") { +>typeof strOrNum === "number" : boolean +>typeof strOrNum : string +>strOrNum : string | number + + num = strOrNum; // number +>num = strOrNum : number +>num : number +>strOrNum : number +} +else { + str === strOrNum; // string +>str === strOrNum : boolean +>str : string +>strOrNum : string +} +if (typeof strOrBool === "number") { +>typeof strOrBool === "number" : boolean +>typeof strOrBool : string +>strOrBool : string | boolean + + num = strOrBool; // number +>num = strOrBool : number +>num : number +>strOrBool : number +} +else { + var y: string | boolean = strOrBool; // string | boolean +>y : string | boolean +>strOrBool : string | boolean +} +if (typeof numOrBool === "number") { +>typeof numOrBool === "number" : boolean +>typeof numOrBool : string +>numOrBool : number | boolean + + num = numOrBool; // number +>num = numOrBool : number +>num : number +>numOrBool : number +} +else { + var x: number | boolean = numOrBool; // number | boolean +>x : number | boolean +>numOrBool : boolean +} +if (typeof strOrNumOrBool === "number") { +>typeof strOrNumOrBool === "number" : boolean +>typeof strOrNumOrBool : string +>strOrNumOrBool : string | number | boolean + + num = strOrNumOrBool; // number +>num = strOrNumOrBool : number +>num : number +>strOrNumOrBool : number +} +else { + strOrBool = strOrNumOrBool; // string | boolean +>strOrBool = strOrNumOrBool : string | boolean +>strOrBool : string | boolean +>strOrNumOrBool : string | boolean +} +if (typeof numOrC === "number") { +>typeof numOrC === "number" : boolean +>typeof numOrC : string +>numOrC : number | C + + num = numOrC; // number +>num = numOrC : number +>num : number +>numOrC : number +} +else { + c = numOrC; // C +>c = numOrC : C +>c : C +>numOrC : C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "number") { +>typeof strOrNum !== "number" : boolean +>typeof strOrNum : string +>strOrNum : string | number + + str === strOrNum; // string +>str === strOrNum : boolean +>str : string +>strOrNum : string +} +else { + num = strOrNum; // number +>num = strOrNum : number +>num : number +>strOrNum : number +} +if (typeof strOrBool !== "number") { +>typeof strOrBool !== "number" : boolean +>typeof strOrBool : string +>strOrBool : string | boolean + + var y: string | boolean = strOrBool; // string | boolean +>y : string | boolean +>strOrBool : string | boolean +} +else { + num = strOrBool; // number +>num = strOrBool : number +>num : number +>strOrBool : number +} +if (typeof numOrBool !== "number") { +>typeof numOrBool !== "number" : boolean +>typeof numOrBool : string +>numOrBool : number | boolean + + var x: number | boolean = numOrBool; // number | boolean +>x : number | boolean +>numOrBool : boolean +} +else { + num = numOrBool; // number +>num = numOrBool : number +>num : number +>numOrBool : number +} +if (typeof strOrNumOrBool !== "number") { +>typeof strOrNumOrBool !== "number" : boolean +>typeof strOrNumOrBool : string +>strOrNumOrBool : string | number | boolean + + strOrBool = strOrNumOrBool; // string | boolean +>strOrBool = strOrNumOrBool : string | boolean +>strOrBool : string | boolean +>strOrNumOrBool : string | boolean +} +else { + num = strOrNumOrBool; // number +>num = strOrNumOrBool : number +>num : number +>strOrNumOrBool : number +} +if (typeof numOrC !== "number") { +>typeof numOrC !== "number" : boolean +>typeof numOrC : string +>numOrC : number | C + + c = numOrC; // C +>c = numOrC : C +>c : C +>numOrC : C +} +else { + num = numOrC; // number +>num = numOrC : number +>num : number +>numOrC : number +} diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfOther.js b/tests/baselines/reference/typeGuardOfFormTypeOfOther.js new file mode 100644 index 00000000000..155b2b9ce57 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfOther.js @@ -0,0 +1,148 @@ +//// [typeGuardOfFormTypeOfOther.ts] +class C { private p: string }; + +var str: string; +var bool: boolean; +var num: number; +var strOrNum: string | number; +var strOrBool: string | boolean; +var numOrBool: number | boolean +var strOrNumOrBool: string | number | boolean; +var strOrC: string | C; +var numOrC: number | C; +var boolOrC: boolean | C; +var emptyObj: {}; +var c: C; + +// A type guard of the form typeof x === s, +// where s is a string literal with any value but 'string', 'number' or 'boolean', +// - when true, removes the primitive types string, number, and boolean from the type of x, or +// - when false, has no effect on the type of x. + +if (typeof strOrNumOrBool === "Object") { + emptyObj = strOrNumOrBool; // {} +} +else { + var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean +} +if (typeof strOrC === "Object") { + c = strOrC; // C +} +else { + var r2: string | C = strOrC; // string | C +} +if (typeof numOrC === "Object") { + c = numOrC; // C +} +else { + var r3: number | C = numOrC; // number | C +} +if (typeof boolOrC === "Object") { + c = boolOrC; // C +} +else { + var r4: boolean | C = boolOrC; // boolean | C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNumOrBool !== "Object") { + var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean +} +else { + emptyObj = strOrNumOrBool; // {} +} +if (typeof strOrC !== "Object") { + var r2: string | C = strOrC; // string | C +} +else { + c = strOrC; // C +} +if (typeof numOrC !== "Object") { + var r3: number | C = numOrC; // number | C +} +else { + c = numOrC; // C +} +if (typeof boolOrC !== "Object") { + var r4: boolean | C = boolOrC; // boolean | C +} +else { + c = boolOrC; // C +} + +//// [typeGuardOfFormTypeOfOther.js] +var C = (function () { + function C() { + } + return C; +})(); +; +var str; +var bool; +var num; +var strOrNum; +var strOrBool; +var numOrBool; +var strOrNumOrBool; +var strOrC; +var numOrC; +var boolOrC; +var emptyObj; +var c; +// A type guard of the form typeof x === s, +// where s is a string literal with any value but 'string', 'number' or 'boolean', +// - when true, removes the primitive types string, number, and boolean from the type of x, or +// - when false, has no effect on the type of x. +if (typeof strOrNumOrBool === "Object") { + emptyObj = strOrNumOrBool; // {} +} +else { + var r1 = strOrNumOrBool; // string | number | boolean +} +if (typeof strOrC === "Object") { + c = strOrC; // C +} +else { + var r2 = strOrC; // string | C +} +if (typeof numOrC === "Object") { + c = numOrC; // C +} +else { + var r3 = numOrC; // number | C +} +if (typeof boolOrC === "Object") { + c = boolOrC; // C +} +else { + var r4 = boolOrC; // boolean | C +} +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNumOrBool !== "Object") { + var r1 = strOrNumOrBool; // string | number | boolean +} +else { + emptyObj = strOrNumOrBool; // {} +} +if (typeof strOrC !== "Object") { + var r2 = strOrC; // string | C +} +else { + c = strOrC; // C +} +if (typeof numOrC !== "Object") { + var r3 = numOrC; // number | C +} +else { + c = numOrC; // C +} +if (typeof boolOrC !== "Object") { + var r4 = boolOrC; // boolean | C +} +else { + c = boolOrC; // C +} diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfOther.types b/tests/baselines/reference/typeGuardOfFormTypeOfOther.types new file mode 100644 index 00000000000..729c255b147 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfOther.types @@ -0,0 +1,180 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts === +class C { private p: string }; +>C : C +>p : string + +var str: string; +>str : string + +var bool: boolean; +>bool : boolean + +var num: number; +>num : number + +var strOrNum: string | number; +>strOrNum : string | number + +var strOrBool: string | boolean; +>strOrBool : string | boolean + +var numOrBool: number | boolean +>numOrBool : number | boolean + +var strOrNumOrBool: string | number | boolean; +>strOrNumOrBool : string | number | boolean + +var strOrC: string | C; +>strOrC : string | C +>C : C + +var numOrC: number | C; +>numOrC : number | C +>C : C + +var boolOrC: boolean | C; +>boolOrC : boolean | C +>C : C + +var emptyObj: {}; +>emptyObj : {} + +var c: C; +>c : C +>C : C + +// A type guard of the form typeof x === s, +// where s is a string literal with any value but 'string', 'number' or 'boolean', +// - when true, removes the primitive types string, number, and boolean from the type of x, or +// - when false, has no effect on the type of x. + +if (typeof strOrNumOrBool === "Object") { +>typeof strOrNumOrBool === "Object" : boolean +>typeof strOrNumOrBool : string +>strOrNumOrBool : string | number | boolean + + emptyObj = strOrNumOrBool; // {} +>emptyObj = strOrNumOrBool : {} +>emptyObj : {} +>strOrNumOrBool : {} +} +else { + var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean +>r1 : string | number | boolean +>strOrNumOrBool : string | number | boolean +} +if (typeof strOrC === "Object") { +>typeof strOrC === "Object" : boolean +>typeof strOrC : string +>strOrC : string | C + + c = strOrC; // C +>c = strOrC : C +>c : C +>strOrC : C +} +else { + var r2: string | C = strOrC; // string | C +>r2 : string | C +>C : C +>strOrC : string | C +} +if (typeof numOrC === "Object") { +>typeof numOrC === "Object" : boolean +>typeof numOrC : string +>numOrC : number | C + + c = numOrC; // C +>c = numOrC : C +>c : C +>numOrC : C +} +else { + var r3: number | C = numOrC; // number | C +>r3 : number | C +>C : C +>numOrC : number | C +} +if (typeof boolOrC === "Object") { +>typeof boolOrC === "Object" : boolean +>typeof boolOrC : string +>boolOrC : boolean | C + + c = boolOrC; // C +>c = boolOrC : C +>c : C +>boolOrC : C +} +else { + var r4: boolean | C = boolOrC; // boolean | C +>r4 : boolean | C +>C : C +>boolOrC : boolean | C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNumOrBool !== "Object") { +>typeof strOrNumOrBool !== "Object" : boolean +>typeof strOrNumOrBool : string +>strOrNumOrBool : string | number | boolean + + var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean +>r1 : string | number | boolean +>strOrNumOrBool : string | number | boolean +} +else { + emptyObj = strOrNumOrBool; // {} +>emptyObj = strOrNumOrBool : {} +>emptyObj : {} +>strOrNumOrBool : {} +} +if (typeof strOrC !== "Object") { +>typeof strOrC !== "Object" : boolean +>typeof strOrC : string +>strOrC : string | C + + var r2: string | C = strOrC; // string | C +>r2 : string | C +>C : C +>strOrC : string | C +} +else { + c = strOrC; // C +>c = strOrC : C +>c : C +>strOrC : C +} +if (typeof numOrC !== "Object") { +>typeof numOrC !== "Object" : boolean +>typeof numOrC : string +>numOrC : number | C + + var r3: number | C = numOrC; // number | C +>r3 : number | C +>C : C +>numOrC : number | C +} +else { + c = numOrC; // C +>c = numOrC : C +>c : C +>numOrC : C +} +if (typeof boolOrC !== "Object") { +>typeof boolOrC !== "Object" : boolean +>typeof boolOrC : string +>boolOrC : boolean | C + + var r4: boolean | C = boolOrC; // boolean | C +>r4 : boolean | C +>C : C +>boolOrC : boolean | C +} +else { + c = boolOrC; // C +>c = boolOrC : C +>c : C +>boolOrC : C +} diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfString.js b/tests/baselines/reference/typeGuardOfFormTypeOfString.js new file mode 100644 index 00000000000..311f499e171 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfString.js @@ -0,0 +1,169 @@ +//// [typeGuardOfFormTypeOfString.ts] +class C { private p: string }; + +var str: string; +var bool: boolean; +var num: number; +var strOrNum: string | number; +var strOrBool: string | boolean; +var numOrBool: number | boolean +var strOrNumOrBool: string | number | boolean; +var strOrC: string | C; +var numOrC: number | C; +var boolOrC: boolean | C; +var c: C; + +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "string") { + str = strOrNum; // string +} +else { + num === strOrNum; // number +} +if (typeof strOrBool === "string") { + str = strOrBool; // string +} +else { + bool = strOrBool; // boolean +} +if (typeof numOrBool === "string") { + str = numOrBool; // string +} +else { + var x : number | boolean = numOrBool; // number | boolean +} +if (typeof strOrNumOrBool === "string") { + str = strOrNumOrBool; // string +} +else { + numOrBool = strOrNumOrBool; // number | boolean +} +if (typeof strOrC === "string") { + str = strOrC; // string +} +else { + c = strOrC; // C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "string") { + num === strOrNum; // number +} +else { + str = strOrNum; // string +} +if (typeof strOrBool !== "string") { + bool = strOrBool; // boolean +} +else { + str = strOrBool; // string +} +if (typeof numOrBool !== "string") { + var x: number | boolean = numOrBool; // number | boolean +} +else { + str = numOrBool; // string +} +if (typeof strOrNumOrBool !== "string") { + numOrBool = strOrNumOrBool; // number | boolean +} +else { + str = strOrNumOrBool; // string +} +if (typeof strOrC !== "string") { + c = strOrC; // C +} +else { + str = strOrC; // string +} + +//// [typeGuardOfFormTypeOfString.js] +var C = (function () { + function C() { + } + return C; +})(); +; +var str; +var bool; +var num; +var strOrNum; +var strOrBool; +var numOrBool; +var strOrNumOrBool; +var strOrC; +var numOrC; +var boolOrC; +var c; +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "string") { + str = strOrNum; // string +} +else { + num === strOrNum; // number +} +if (typeof strOrBool === "string") { + str = strOrBool; // string +} +else { + bool = strOrBool; // boolean +} +if (typeof numOrBool === "string") { + str = numOrBool; // string +} +else { + var x = numOrBool; // number | boolean +} +if (typeof strOrNumOrBool === "string") { + str = strOrNumOrBool; // string +} +else { + numOrBool = strOrNumOrBool; // number | boolean +} +if (typeof strOrC === "string") { + str = strOrC; // string +} +else { + c = strOrC; // C +} +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "string") { + num === strOrNum; // number +} +else { + str = strOrNum; // string +} +if (typeof strOrBool !== "string") { + bool = strOrBool; // boolean +} +else { + str = strOrBool; // string +} +if (typeof numOrBool !== "string") { + var x = numOrBool; // number | boolean +} +else { + str = numOrBool; // string +} +if (typeof strOrNumOrBool !== "string") { + numOrBool = strOrNumOrBool; // number | boolean +} +else { + str = strOrNumOrBool; // string +} +if (typeof strOrC !== "string") { + c = strOrC; // C +} +else { + str = strOrC; // string +} diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfString.types b/tests/baselines/reference/typeGuardOfFormTypeOfString.types new file mode 100644 index 00000000000..e6d90b74c66 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfString.types @@ -0,0 +1,208 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfString.ts === +class C { private p: string }; +>C : C +>p : string + +var str: string; +>str : string + +var bool: boolean; +>bool : boolean + +var num: number; +>num : number + +var strOrNum: string | number; +>strOrNum : string | number + +var strOrBool: string | boolean; +>strOrBool : string | boolean + +var numOrBool: number | boolean +>numOrBool : number | boolean + +var strOrNumOrBool: string | number | boolean; +>strOrNumOrBool : string | number | boolean + +var strOrC: string | C; +>strOrC : string | C +>C : C + +var numOrC: number | C; +>numOrC : number | C +>C : C + +var boolOrC: boolean | C; +>boolOrC : boolean | C +>C : C + +var c: C; +>c : C +>C : C + +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "string") { +>typeof strOrNum === "string" : boolean +>typeof strOrNum : string +>strOrNum : string | number + + str = strOrNum; // string +>str = strOrNum : string +>str : string +>strOrNum : string +} +else { + num === strOrNum; // number +>num === strOrNum : boolean +>num : number +>strOrNum : number +} +if (typeof strOrBool === "string") { +>typeof strOrBool === "string" : boolean +>typeof strOrBool : string +>strOrBool : string | boolean + + str = strOrBool; // string +>str = strOrBool : string +>str : string +>strOrBool : string +} +else { + bool = strOrBool; // boolean +>bool = strOrBool : boolean +>bool : boolean +>strOrBool : boolean +} +if (typeof numOrBool === "string") { +>typeof numOrBool === "string" : boolean +>typeof numOrBool : string +>numOrBool : number | boolean + + str = numOrBool; // string +>str = numOrBool : string +>str : string +>numOrBool : string +} +else { + var x : number | boolean = numOrBool; // number | boolean +>x : number | boolean +>numOrBool : number | boolean +} +if (typeof strOrNumOrBool === "string") { +>typeof strOrNumOrBool === "string" : boolean +>typeof strOrNumOrBool : string +>strOrNumOrBool : string | number | boolean + + str = strOrNumOrBool; // string +>str = strOrNumOrBool : string +>str : string +>strOrNumOrBool : string +} +else { + numOrBool = strOrNumOrBool; // number | boolean +>numOrBool = strOrNumOrBool : number | boolean +>numOrBool : number | boolean +>strOrNumOrBool : number | boolean +} +if (typeof strOrC === "string") { +>typeof strOrC === "string" : boolean +>typeof strOrC : string +>strOrC : string | C + + str = strOrC; // string +>str = strOrC : string +>str : string +>strOrC : string +} +else { + c = strOrC; // C +>c = strOrC : C +>c : C +>strOrC : C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "string") { +>typeof strOrNum !== "string" : boolean +>typeof strOrNum : string +>strOrNum : string | number + + num === strOrNum; // number +>num === strOrNum : boolean +>num : number +>strOrNum : number +} +else { + str = strOrNum; // string +>str = strOrNum : string +>str : string +>strOrNum : string +} +if (typeof strOrBool !== "string") { +>typeof strOrBool !== "string" : boolean +>typeof strOrBool : string +>strOrBool : string | boolean + + bool = strOrBool; // boolean +>bool = strOrBool : boolean +>bool : boolean +>strOrBool : boolean +} +else { + str = strOrBool; // string +>str = strOrBool : string +>str : string +>strOrBool : string +} +if (typeof numOrBool !== "string") { +>typeof numOrBool !== "string" : boolean +>typeof numOrBool : string +>numOrBool : number | boolean + + var x: number | boolean = numOrBool; // number | boolean +>x : number | boolean +>numOrBool : number | boolean +} +else { + str = numOrBool; // string +>str = numOrBool : string +>str : string +>numOrBool : string +} +if (typeof strOrNumOrBool !== "string") { +>typeof strOrNumOrBool !== "string" : boolean +>typeof strOrNumOrBool : string +>strOrNumOrBool : string | number | boolean + + numOrBool = strOrNumOrBool; // number | boolean +>numOrBool = strOrNumOrBool : number | boolean +>numOrBool : number | boolean +>strOrNumOrBool : number | boolean +} +else { + str = strOrNumOrBool; // string +>str = strOrNumOrBool : string +>str : string +>strOrNumOrBool : string +} +if (typeof strOrC !== "string") { +>typeof strOrC !== "string" : boolean +>typeof strOrC : string +>strOrC : string | C + + c = strOrC; // C +>c = strOrC : C +>c : C +>strOrC : C +} +else { + str = strOrC; // string +>str = strOrC : string +>str : string +>strOrC : string +} diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfBoolean.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfBoolean.ts new file mode 100644 index 00000000000..dc6166852ae --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfBoolean.ts @@ -0,0 +1,82 @@ +class C { private p: string }; + +var str: string; +var bool: boolean; +var num: number; +var strOrNum: string | number; +var strOrBool: string | boolean; +var numOrBool: number | boolean +var strOrNumOrBool: string | number | boolean; +var strOrC: string | C; +var numOrC: number | C; +var boolOrC: boolean | C; +var c: C; + +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "boolean") { + bool = strOrNum; // boolean +} +else { + var z: string | number = strOrNum; // string | number +} +if (typeof strOrBool === "boolean") { + bool = strOrBool; // boolean +} +else { + str = strOrBool; // string +} +if (typeof numOrBool === "boolean") { + bool = numOrBool; // boolean +} +else { + num = numOrBool; // number +} +if (typeof strOrNumOrBool === "boolean") { + bool = strOrNumOrBool; // boolean +} +else { + strOrNum = strOrNumOrBool; // string | number +} +if (typeof boolOrC === "boolean") { + bool = boolOrC; // boolean +} +else { + c = boolOrC; // C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "boolean") { + var z: string | number = strOrNum; // string | number +} +else { + bool = strOrNum; // boolean +} +if (typeof strOrBool !== "boolean") { + str = strOrBool; // string +} +else { + bool = strOrBool; // boolean +} +if (typeof numOrBool !== "boolean") { + num = numOrBool; // number +} +else { + bool = numOrBool; // boolean +} +if (typeof strOrNumOrBool !== "boolean") { + strOrNum = strOrNumOrBool; // string | number +} +else { + bool = strOrNumOrBool; // boolean +} +if (typeof boolOrC !== "boolean") { + c = boolOrC; // C +} +else { + bool = boolOrC; // boolean +} \ No newline at end of file diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfNumber.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfNumber.ts new file mode 100644 index 00000000000..b05e5a3a002 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfNumber.ts @@ -0,0 +1,82 @@ +class C { private p: string }; + +var str: string; +var bool: boolean; +var num: number; +var strOrNum: string | number; +var strOrBool: string | boolean; +var numOrBool: number | boolean +var strOrNumOrBool: string | number | boolean; +var strOrC: string | C; +var numOrC: number | C; +var boolOrC: boolean | C; +var c: C; + +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "number") { + num = strOrNum; // number +} +else { + str === strOrNum; // string +} +if (typeof strOrBool === "number") { + num = strOrBool; // number +} +else { + var y: string | boolean = strOrBool; // string | boolean +} +if (typeof numOrBool === "number") { + num = numOrBool; // number +} +else { + var x: number | boolean = numOrBool; // number | boolean +} +if (typeof strOrNumOrBool === "number") { + num = strOrNumOrBool; // number +} +else { + strOrBool = strOrNumOrBool; // string | boolean +} +if (typeof numOrC === "number") { + num = numOrC; // number +} +else { + c = numOrC; // C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "number") { + str === strOrNum; // string +} +else { + num = strOrNum; // number +} +if (typeof strOrBool !== "number") { + var y: string | boolean = strOrBool; // string | boolean +} +else { + num = strOrBool; // number +} +if (typeof numOrBool !== "number") { + var x: number | boolean = numOrBool; // number | boolean +} +else { + num = numOrBool; // number +} +if (typeof strOrNumOrBool !== "number") { + strOrBool = strOrNumOrBool; // string | boolean +} +else { + num = strOrNumOrBool; // number +} +if (typeof numOrC !== "number") { + c = numOrC; // C +} +else { + num = numOrC; // number +} \ No newline at end of file diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts new file mode 100644 index 00000000000..18109890c27 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts @@ -0,0 +1,72 @@ +class C { private p: string }; + +var str: string; +var bool: boolean; +var num: number; +var strOrNum: string | number; +var strOrBool: string | boolean; +var numOrBool: number | boolean +var strOrNumOrBool: string | number | boolean; +var strOrC: string | C; +var numOrC: number | C; +var boolOrC: boolean | C; +var emptyObj: {}; +var c: C; + +// A type guard of the form typeof x === s, +// where s is a string literal with any value but 'string', 'number' or 'boolean', +// - when true, removes the primitive types string, number, and boolean from the type of x, or +// - when false, has no effect on the type of x. + +if (typeof strOrNumOrBool === "Object") { + emptyObj = strOrNumOrBool; // {} +} +else { + var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean +} +if (typeof strOrC === "Object") { + c = strOrC; // C +} +else { + var r2: string | C = strOrC; // string | C +} +if (typeof numOrC === "Object") { + c = numOrC; // C +} +else { + var r3: number | C = numOrC; // number | C +} +if (typeof boolOrC === "Object") { + c = boolOrC; // C +} +else { + var r4: boolean | C = boolOrC; // boolean | C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNumOrBool !== "Object") { + var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean +} +else { + emptyObj = strOrNumOrBool; // {} +} +if (typeof strOrC !== "Object") { + var r2: string | C = strOrC; // string | C +} +else { + c = strOrC; // C +} +if (typeof numOrC !== "Object") { + var r3: number | C = numOrC; // number | C +} +else { + c = numOrC; // C +} +if (typeof boolOrC !== "Object") { + var r4: boolean | C = boolOrC; // boolean | C +} +else { + c = boolOrC; // C +} \ No newline at end of file diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfString.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfString.ts new file mode 100644 index 00000000000..8c6bbdaf907 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfString.ts @@ -0,0 +1,82 @@ +class C { private p: string }; + +var str: string; +var bool: boolean; +var num: number; +var strOrNum: string | number; +var strOrBool: string | boolean; +var numOrBool: number | boolean +var strOrNumOrBool: string | number | boolean; +var strOrC: string | C; +var numOrC: number | C; +var boolOrC: boolean | C; +var c: C; + +// A type guard of the form typeof x === s, +// where s is a string literal with the value 'string', 'number', or 'boolean', +// - when true, narrows the type of x to the given primitive type, or +// - when false, removes the primitive type from the type of x. +if (typeof strOrNum === "string") { + str = strOrNum; // string +} +else { + num === strOrNum; // number +} +if (typeof strOrBool === "string") { + str = strOrBool; // string +} +else { + bool = strOrBool; // boolean +} +if (typeof numOrBool === "string") { + str = numOrBool; // string +} +else { + var x : number | boolean = numOrBool; // number | boolean +} +if (typeof strOrNumOrBool === "string") { + str = strOrNumOrBool; // string +} +else { + numOrBool = strOrNumOrBool; // number | boolean +} +if (typeof strOrC === "string") { + str = strOrC; // string +} +else { + c = strOrC; // C +} + +// A type guard of the form typeof x !== s, where s is a string literal, +// - when true, narrows the type of x by typeof x === s when false, or +// - when false, narrows the type of x by typeof x === s when true. +if (typeof strOrNum !== "string") { + num === strOrNum; // number +} +else { + str = strOrNum; // string +} +if (typeof strOrBool !== "string") { + bool = strOrBool; // boolean +} +else { + str = strOrBool; // string +} +if (typeof numOrBool !== "string") { + var x: number | boolean = numOrBool; // number | boolean +} +else { + str = numOrBool; // string +} +if (typeof strOrNumOrBool !== "string") { + numOrBool = strOrNumOrBool; // number | boolean +} +else { + str = strOrNumOrBool; // string +} +if (typeof strOrC !== "string") { + c = strOrC; // C +} +else { + str = strOrC; // string +} \ No newline at end of file