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/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 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
|
|
|
|
}
|
|
|
|
|
2014-11-14 19:42:31 +01:00
|
|
|
// Narrowing occurs only if target type is a subtype of variable type
|
|
|
|
if (typeof strOrNumOrBool === "Object") {
|
|
|
|
>typeof strOrNumOrBool === "Object" : boolean
|
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
|
|
|
>typeof strOrNumOrBool : string
|
|
|
|
>strOrNumOrBool : string | number | boolean
|
|
|
|
|
2014-11-14 19:42:31 +01:00
|
|
|
var q1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
|
|
|
>q1 : string | number | boolean
|
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
|
|
|
>strOrNumOrBool : string | number | boolean
|
|
|
|
}
|
|
|
|
else {
|
2014-11-14 19:42:31 +01:00
|
|
|
var q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
|
|
|
>q2 : string | number | boolean
|
|
|
|
>strOrNumOrBool : string | number | boolean
|
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
|
|
|
|
|
|
|
// 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.
|
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 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
|
|
|
|
}
|
2014-11-14 19:42:31 +01:00
|
|
|
|
|
|
|
// Narrowing occurs only if target type is a subtype of variable type
|
|
|
|
if (typeof strOrNumOrBool !== "Object") {
|
|
|
|
>typeof strOrNumOrBool !== "Object" : boolean
|
|
|
|
>typeof strOrNumOrBool : string
|
|
|
|
>strOrNumOrBool : string | number | boolean
|
|
|
|
|
|
|
|
var q1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
|
|
|
>q1 : string | number | boolean
|
|
|
|
>strOrNumOrBool : string | number | boolean
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
|
|
|
>q2 : string | number | boolean
|
|
|
|
>strOrNumOrBool : string | number | boolean
|
|
|
|
}
|
|
|
|
|