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.
2014-11-06 03:31:38 +01:00
|
|
|
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfNumber.ts ===
|
|
|
|
class C { private p: string };
|
2015-04-13 23:01:57 +02:00
|
|
|
>C : C, Symbol(C, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 0))
|
|
|
|
>p : string, Symbol(p, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 9))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var str: string;
|
2015-04-13 23:01:57 +02:00
|
|
|
>str : string, Symbol(str, Decl(typeGuardOfFormTypeOfNumber.ts, 2, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var bool: boolean;
|
2015-04-13 23:01:57 +02:00
|
|
|
>bool : boolean, Symbol(bool, Decl(typeGuardOfFormTypeOfNumber.ts, 3, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var num: number;
|
2015-04-13 23:01:57 +02:00
|
|
|
>num : number, Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var strOrNum: string | number;
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrNum : string | number, Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var strOrBool: string | boolean;
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrBool : string | boolean, Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var numOrBool: number | boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>numOrBool : number | boolean, Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var strOrNumOrBool: string | number | boolean;
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrNumOrBool : string | number | boolean, Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var strOrC: string | C;
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrC : string | C, Symbol(strOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 9, 3))
|
|
|
|
>C : C, Symbol(C, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 0))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var numOrC: number | C;
|
2015-04-13 23:01:57 +02:00
|
|
|
>numOrC : number | C, Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
|
|
|
>C : C, Symbol(C, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 0))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var boolOrC: boolean | C;
|
2015-04-13 23:01:57 +02:00
|
|
|
>boolOrC : boolean | C, Symbol(boolOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 11, 3))
|
|
|
|
>C : C, Symbol(C, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 0))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var c: C;
|
2015-04-13 23:01:57 +02:00
|
|
|
>c : C, Symbol(c, Decl(typeGuardOfFormTypeOfNumber.ts, 12, 3))
|
|
|
|
>C : C, Symbol(C, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 0))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
// 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
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrNum : string | number, Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
2015-04-13 21:36:11 +02:00
|
|
|
>"number" : string
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
num = strOrNum; // number
|
|
|
|
>num = strOrNum : number
|
2015-04-13 23:01:57 +02:00
|
|
|
>num : number, Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
|
|
>strOrNum : number, Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
str === strOrNum; // string
|
|
|
|
>str === strOrNum : boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>str : string, Symbol(str, Decl(typeGuardOfFormTypeOfNumber.ts, 2, 3))
|
|
|
|
>strOrNum : string, Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
if (typeof numOrBool === "number") {
|
|
|
|
>typeof numOrBool === "number" : boolean
|
|
|
|
>typeof numOrBool : string
|
2015-04-13 23:01:57 +02:00
|
|
|
>numOrBool : number | boolean, Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
2015-04-13 21:36:11 +02:00
|
|
|
>"number" : string
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
num = numOrBool; // number
|
|
|
|
>num = numOrBool : number
|
2015-04-13 23:01:57 +02:00
|
|
|
>num : number, Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
|
|
>numOrBool : number, Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var x: number | boolean = numOrBool; // number | boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>x : number | boolean, Symbol(x, Decl(typeGuardOfFormTypeOfNumber.ts, 28, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 61, 7))
|
|
|
|
>numOrBool : boolean, Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
if (typeof strOrNumOrBool === "number") {
|
|
|
|
>typeof strOrNumOrBool === "number" : boolean
|
|
|
|
>typeof strOrNumOrBool : string
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrNumOrBool : string | number | boolean, Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
2015-04-13 21:36:11 +02:00
|
|
|
>"number" : string
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
num = strOrNumOrBool; // number
|
|
|
|
>num = strOrNumOrBool : number
|
2015-04-13 23:01:57 +02:00
|
|
|
>num : number, Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
|
|
>strOrNumOrBool : number, Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
strOrBool = strOrNumOrBool; // string | boolean
|
|
|
|
>strOrBool = strOrNumOrBool : string | boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrBool : string | boolean, Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
|
|
>strOrNumOrBool : string | boolean, Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
if (typeof numOrC === "number") {
|
|
|
|
>typeof numOrC === "number" : boolean
|
|
|
|
>typeof numOrC : string
|
2015-04-13 23:01:57 +02:00
|
|
|
>numOrC : number | C, Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
2015-04-13 21:36:11 +02:00
|
|
|
>"number" : string
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
num = numOrC; // number
|
|
|
|
>num = numOrC : number
|
2015-04-13 23:01:57 +02:00
|
|
|
>num : number, Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
|
|
>numOrC : number, Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
c = numOrC; // C
|
|
|
|
>c = numOrC : C
|
2015-04-13 23:01:57 +02:00
|
|
|
>c : C, Symbol(c, Decl(typeGuardOfFormTypeOfNumber.ts, 12, 3))
|
|
|
|
>numOrC : C, Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
|
2014-11-14 19:42:31 +01:00
|
|
|
// Narrowing occurs only if target type is a subtype of variable type
|
|
|
|
if (typeof strOrBool === "number") {
|
|
|
|
>typeof strOrBool === "number" : boolean
|
|
|
|
>typeof strOrBool : string
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrBool : string | boolean, Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
2015-04-13 21:36:11 +02:00
|
|
|
>"number" : string
|
2014-11-14 19:42:31 +01:00
|
|
|
|
|
|
|
var y1: string | boolean = strOrBool; // string | boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>y1 : string | boolean, Symbol(y1, Decl(typeGuardOfFormTypeOfNumber.ts, 45, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 81, 7))
|
|
|
|
>strOrBool : string | boolean, Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
2014-11-14 19:42:31 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var y2: string | boolean = strOrBool; // string | boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>y2 : string | boolean, Symbol(y2, Decl(typeGuardOfFormTypeOfNumber.ts, 48, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 84, 7))
|
|
|
|
>strOrBool : string | boolean, Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
2014-11-14 19:42:31 +01:00
|
|
|
}
|
|
|
|
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
// 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
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrNum : string | number, Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
2015-04-13 21:36:11 +02:00
|
|
|
>"number" : string
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
str === strOrNum; // string
|
|
|
|
>str === strOrNum : boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>str : string, Symbol(str, Decl(typeGuardOfFormTypeOfNumber.ts, 2, 3))
|
|
|
|
>strOrNum : string, Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
num = strOrNum; // number
|
|
|
|
>num = strOrNum : number
|
2015-04-13 23:01:57 +02:00
|
|
|
>num : number, Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
|
|
>strOrNum : number, Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
if (typeof numOrBool !== "number") {
|
|
|
|
>typeof numOrBool !== "number" : boolean
|
|
|
|
>typeof numOrBool : string
|
2015-04-13 23:01:57 +02:00
|
|
|
>numOrBool : number | boolean, Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
2015-04-13 21:36:11 +02:00
|
|
|
>"number" : string
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
var x: number | boolean = numOrBool; // number | boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>x : number | boolean, Symbol(x, Decl(typeGuardOfFormTypeOfNumber.ts, 28, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 61, 7))
|
|
|
|
>numOrBool : boolean, Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
num = numOrBool; // number
|
|
|
|
>num = numOrBool : number
|
2015-04-13 23:01:57 +02:00
|
|
|
>num : number, Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
|
|
>numOrBool : number, Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
if (typeof strOrNumOrBool !== "number") {
|
|
|
|
>typeof strOrNumOrBool !== "number" : boolean
|
|
|
|
>typeof strOrNumOrBool : string
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrNumOrBool : string | number | boolean, Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
2015-04-13 21:36:11 +02:00
|
|
|
>"number" : string
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
strOrBool = strOrNumOrBool; // string | boolean
|
|
|
|
>strOrBool = strOrNumOrBool : string | boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrBool : string | boolean, Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
|
|
>strOrNumOrBool : string | boolean, Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
num = strOrNumOrBool; // number
|
|
|
|
>num = strOrNumOrBool : number
|
2015-04-13 23:01:57 +02:00
|
|
|
>num : number, Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
|
|
>strOrNumOrBool : number, Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
if (typeof numOrC !== "number") {
|
|
|
|
>typeof numOrC !== "number" : boolean
|
|
|
|
>typeof numOrC : string
|
2015-04-13 23:01:57 +02:00
|
|
|
>numOrC : number | C, Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
2015-04-13 21:36:11 +02:00
|
|
|
>"number" : string
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
|
|
|
|
c = numOrC; // C
|
|
|
|
>c = numOrC : C
|
2015-04-13 23:01:57 +02:00
|
|
|
>c : C, Symbol(c, Decl(typeGuardOfFormTypeOfNumber.ts, 12, 3))
|
|
|
|
>numOrC : C, Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
num = numOrC; // number
|
|
|
|
>num = numOrC : number
|
2015-04-13 23:01:57 +02:00
|
|
|
>num : number, Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
|
|
>numOrC : number, Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
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.
2014-11-06 03:31:38 +01:00
|
|
|
}
|
2014-11-14 19:42:31 +01:00
|
|
|
|
|
|
|
// Narrowing occurs only if target type is a subtype of variable type
|
|
|
|
if (typeof strOrBool !== "number") {
|
|
|
|
>typeof strOrBool !== "number" : boolean
|
|
|
|
>typeof strOrBool : string
|
2015-04-13 23:01:57 +02:00
|
|
|
>strOrBool : string | boolean, Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
2015-04-13 21:36:11 +02:00
|
|
|
>"number" : string
|
2014-11-14 19:42:31 +01:00
|
|
|
|
|
|
|
var y1: string | boolean = strOrBool; // string | boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>y1 : string | boolean, Symbol(y1, Decl(typeGuardOfFormTypeOfNumber.ts, 45, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 81, 7))
|
|
|
|
>strOrBool : string | boolean, Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
2014-11-14 19:42:31 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var y2: string | boolean = strOrBool; // string | boolean
|
2015-04-13 23:01:57 +02:00
|
|
|
>y2 : string | boolean, Symbol(y2, Decl(typeGuardOfFormTypeOfNumber.ts, 48, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 84, 7))
|
|
|
|
>strOrBool : string | boolean, Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
2014-11-14 19:42:31 +01:00
|
|
|
}
|
|
|
|
|