Test cases for typeguards of form typeof x === s and typeof x !== s

•	A type guard of the form typeof x === s, where s is a string literal with the value ‘string’, ‘number’, or ‘boolean’,
o	when true, narrows the type of x to the given primitive type, or
o	when false, removes the primitive type from the type of x.
•	A type guard of the form typeof x === s, where s is a string literal with any value but ‘string’, ‘number’, or ‘boolean’,
o	when true, removes the primitive types string, number, and boolean from the type of x, or
o	when false, has no effect on the type of x.
•	A type guard of the form typeof x !== s, where s is a string literal,
o	when true, narrows the type of x by typeof x === s when false, or
o	when false, narrows the type of x by typeof x === s when true.
This commit is contained in:
Sheetal Nandi
2014-11-05 18:31:38 -08:00
parent 5961ed7154
commit 7ebf5371a5
12 changed files with 1775 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}