175 lines
7.3 KiB
Text
175 lines
7.3 KiB
Text
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfNumber.ts ===
|
|
class C { private p: string };
|
|
>C : Symbol(C, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 0))
|
|
>p : Symbol(p, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 9))
|
|
|
|
var str: string;
|
|
>str : Symbol(str, Decl(typeGuardOfFormTypeOfNumber.ts, 2, 3))
|
|
|
|
var bool: boolean;
|
|
>bool : Symbol(bool, Decl(typeGuardOfFormTypeOfNumber.ts, 3, 3))
|
|
|
|
var num: number;
|
|
>num : Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
|
|
var strOrNum: string | number;
|
|
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
|
|
|
var strOrBool: string | boolean;
|
|
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
|
|
var numOrBool: number | boolean
|
|
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
|
|
|
var strOrNumOrBool: string | number | boolean;
|
|
>strOrNumOrBool : Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
|
|
|
var strOrC: string | C;
|
|
>strOrC : Symbol(strOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 9, 3))
|
|
>C : Symbol(C, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 0))
|
|
|
|
var numOrC: number | C;
|
|
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
|
>C : Symbol(C, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 0))
|
|
|
|
var boolOrC: boolean | C;
|
|
>boolOrC : Symbol(boolOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 11, 3))
|
|
>C : Symbol(C, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 0))
|
|
|
|
var c: C;
|
|
>c : Symbol(c, Decl(typeGuardOfFormTypeOfNumber.ts, 12, 3))
|
|
>C : Symbol(C, Decl(typeGuardOfFormTypeOfNumber.ts, 0, 0))
|
|
|
|
// 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") {
|
|
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
|
|
|
num = strOrNum; // number
|
|
>num : Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
|
}
|
|
else {
|
|
str === strOrNum; // string
|
|
>str : Symbol(str, Decl(typeGuardOfFormTypeOfNumber.ts, 2, 3))
|
|
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
|
}
|
|
if (typeof numOrBool === "number") {
|
|
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
|
|
|
num = numOrBool; // number
|
|
>num : Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
|
}
|
|
else {
|
|
var x: number | boolean = numOrBool; // number | boolean
|
|
>x : Symbol(x, Decl(typeGuardOfFormTypeOfNumber.ts, 28, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 61, 7))
|
|
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
|
}
|
|
if (typeof strOrNumOrBool === "number") {
|
|
>strOrNumOrBool : Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
|
|
|
num = strOrNumOrBool; // number
|
|
>num : Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
>strOrNumOrBool : Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
|
}
|
|
else {
|
|
strOrBool = strOrNumOrBool; // string | boolean
|
|
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
>strOrNumOrBool : Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
|
}
|
|
if (typeof numOrC === "number") {
|
|
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
|
|
|
num = numOrC; // number
|
|
>num : Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
|
}
|
|
else {
|
|
c = numOrC; // C
|
|
>c : Symbol(c, Decl(typeGuardOfFormTypeOfNumber.ts, 12, 3))
|
|
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
|
}
|
|
|
|
// Narrowing occurs only if target type is a subtype of variable type
|
|
if (typeof strOrBool === "number") {
|
|
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
|
|
var y1: string | boolean = strOrBool; // string | boolean
|
|
>y1 : Symbol(y1, Decl(typeGuardOfFormTypeOfNumber.ts, 45, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 81, 7))
|
|
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
}
|
|
else {
|
|
var y2: string | boolean = strOrBool; // string | boolean
|
|
>y2 : Symbol(y2, Decl(typeGuardOfFormTypeOfNumber.ts, 48, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 84, 7))
|
|
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
}
|
|
|
|
// 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") {
|
|
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
|
|
|
str === strOrNum; // string
|
|
>str : Symbol(str, Decl(typeGuardOfFormTypeOfNumber.ts, 2, 3))
|
|
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
|
}
|
|
else {
|
|
num = strOrNum; // number
|
|
>num : Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfNumber.ts, 5, 3))
|
|
}
|
|
if (typeof numOrBool !== "number") {
|
|
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
|
|
|
var x: number | boolean = numOrBool; // number | boolean
|
|
>x : Symbol(x, Decl(typeGuardOfFormTypeOfNumber.ts, 28, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 61, 7))
|
|
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
|
}
|
|
else {
|
|
num = numOrBool; // number
|
|
>num : Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 7, 3))
|
|
}
|
|
if (typeof strOrNumOrBool !== "number") {
|
|
>strOrNumOrBool : Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
|
|
|
strOrBool = strOrNumOrBool; // string | boolean
|
|
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
>strOrNumOrBool : Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
|
}
|
|
else {
|
|
num = strOrNumOrBool; // number
|
|
>num : Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
>strOrNumOrBool : Symbol(strOrNumOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 8, 3))
|
|
}
|
|
if (typeof numOrC !== "number") {
|
|
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
|
|
|
c = numOrC; // C
|
|
>c : Symbol(c, Decl(typeGuardOfFormTypeOfNumber.ts, 12, 3))
|
|
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
|
}
|
|
else {
|
|
num = numOrC; // number
|
|
>num : Symbol(num, Decl(typeGuardOfFormTypeOfNumber.ts, 4, 3))
|
|
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfNumber.ts, 10, 3))
|
|
}
|
|
|
|
// Narrowing occurs only if target type is a subtype of variable type
|
|
if (typeof strOrBool !== "number") {
|
|
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
|
|
var y1: string | boolean = strOrBool; // string | boolean
|
|
>y1 : Symbol(y1, Decl(typeGuardOfFormTypeOfNumber.ts, 45, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 81, 7))
|
|
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
}
|
|
else {
|
|
var y2: string | boolean = strOrBool; // string | boolean
|
|
>y2 : Symbol(y2, Decl(typeGuardOfFormTypeOfNumber.ts, 48, 7), Decl(typeGuardOfFormTypeOfNumber.ts, 84, 7))
|
|
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfNumber.ts, 6, 3))
|
|
}
|
|
|