Typeguards of form !expr

•	A type guard of the form !expr
o	when true, narrows the type of x by expr when false, or
o	when false, narrows the type of x by expr when true.
This commit is contained in:
Sheetal Nandi 2014-11-06 13:00:44 -08:00
parent c9a03dc659
commit 52a8560298
3 changed files with 320 additions and 0 deletions

View file

@ -0,0 +1,107 @@
//// [typeGuardOfFormNotExpr.ts]
var str: string;
var bool: boolean;
var num: number;
var strOrNum: string | number;
var strOrNumOrBool: string | number | boolean;
var numOrBool: number | boolean;
// A type guard of the form !expr
// - when true, narrows the type of x by expr when false, or
// - when false, narrows the type of x by expr when true.
// !typeguard1
if (!(typeof strOrNum === "string")) {
num === strOrNum; // number
}
else {
str = strOrNum; // string
}
// !(typeguard1 || typeguard2)
if (!(typeof strOrNumOrBool === "string" || typeof strOrNumOrBool === "number")) {
bool = strOrNumOrBool; // boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
}
// !(typeguard1) || !(typeguard2)
if (!(typeof strOrNumOrBool !== "string") || !(typeof strOrNumOrBool !== "number")) {
strOrNum = strOrNumOrBool; // string | number
}
else {
bool = strOrNumOrBool; // boolean
}
// !(typeguard1 && typeguard2)
if (!(typeof strOrNumOrBool !== "string" && typeof strOrNumOrBool !== "number")) {
strOrNum = strOrNumOrBool; // string | number
}
else {
bool = strOrNumOrBool; // boolean
}
// !(typeguard1) && !(typeguard2)
if (!(typeof strOrNumOrBool === "string") && !(typeof strOrNumOrBool === "number")) {
bool = strOrNumOrBool; // boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
}
// !(typeguard1) && simpleExpr
if (!(typeof strOrNumOrBool === "string") && numOrBool !== strOrNumOrBool) {
numOrBool = strOrNumOrBool; // number | boolean
}
else {
var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean
}
//// [typeGuardOfFormNotExpr.js]
var str;
var bool;
var num;
var strOrNum;
var strOrNumOrBool;
var numOrBool;
// A type guard of the form !expr
// - when true, narrows the type of x by expr when false, or
// - when false, narrows the type of x by expr when true.
// !typeguard1
if (!(typeof strOrNum === "string")) {
num === strOrNum; // number
}
else {
str = strOrNum; // string
}
// !(typeguard1 || typeguard2)
if (!(typeof strOrNumOrBool === "string" || typeof strOrNumOrBool === "number")) {
bool = strOrNumOrBool; // boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
}
// !(typeguard1) || !(typeguard2)
if (!(typeof strOrNumOrBool !== "string") || !(typeof strOrNumOrBool !== "number")) {
strOrNum = strOrNumOrBool; // string | number
}
else {
bool = strOrNumOrBool; // boolean
}
// !(typeguard1 && typeguard2)
if (!(typeof strOrNumOrBool !== "string" && typeof strOrNumOrBool !== "number")) {
strOrNum = strOrNumOrBool; // string | number
}
else {
bool = strOrNumOrBool; // boolean
}
// !(typeguard1) && !(typeguard2)
if (!(typeof strOrNumOrBool === "string") && !(typeof strOrNumOrBool === "number")) {
bool = strOrNumOrBool; // boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
}
// !(typeguard1) && simpleExpr
if (!(typeof strOrNumOrBool === "string") && numOrBool !== strOrNumOrBool) {
numOrBool = strOrNumOrBool; // number | boolean
}
else {
var r1 = strOrNumOrBool; // string | number | boolean
}

View file

@ -0,0 +1,160 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormNotExpr.ts ===
var str: string;
>str : string
var bool: boolean;
>bool : boolean
var num: number;
>num : number
var strOrNum: string | number;
>strOrNum : string | number
var strOrNumOrBool: string | number | boolean;
>strOrNumOrBool : string | number | boolean
var numOrBool: number | boolean;
>numOrBool : number | boolean
// A type guard of the form !expr
// - when true, narrows the type of x by expr when false, or
// - when false, narrows the type of x by expr when true.
// !typeguard1
if (!(typeof strOrNum === "string")) {
>!(typeof strOrNum === "string") : boolean
>(typeof strOrNum === "string") : boolean
>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
}
// !(typeguard1 || typeguard2)
if (!(typeof strOrNumOrBool === "string" || typeof strOrNumOrBool === "number")) {
>!(typeof strOrNumOrBool === "string" || typeof strOrNumOrBool === "number") : boolean
>(typeof strOrNumOrBool === "string" || typeof strOrNumOrBool === "number") : boolean
>typeof strOrNumOrBool === "string" || typeof strOrNumOrBool === "number" : boolean
>typeof strOrNumOrBool === "string" : boolean
>typeof strOrNumOrBool : string
>strOrNumOrBool : string | number | boolean
>typeof strOrNumOrBool === "number" : boolean
>typeof strOrNumOrBool : string
>strOrNumOrBool : 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
}
// !(typeguard1) || !(typeguard2)
if (!(typeof strOrNumOrBool !== "string") || !(typeof strOrNumOrBool !== "number")) {
>!(typeof strOrNumOrBool !== "string") || !(typeof strOrNumOrBool !== "number") : boolean
>!(typeof strOrNumOrBool !== "string") : boolean
>(typeof strOrNumOrBool !== "string") : boolean
>typeof strOrNumOrBool !== "string" : boolean
>typeof strOrNumOrBool : string
>strOrNumOrBool : string | number | boolean
>!(typeof strOrNumOrBool !== "number") : boolean
>(typeof strOrNumOrBool !== "number") : boolean
>typeof strOrNumOrBool !== "number" : boolean
>typeof strOrNumOrBool : string
>strOrNumOrBool : 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
}
// !(typeguard1 && typeguard2)
if (!(typeof strOrNumOrBool !== "string" && typeof strOrNumOrBool !== "number")) {
>!(typeof strOrNumOrBool !== "string" && typeof strOrNumOrBool !== "number") : boolean
>(typeof strOrNumOrBool !== "string" && typeof strOrNumOrBool !== "number") : boolean
>typeof strOrNumOrBool !== "string" && typeof strOrNumOrBool !== "number" : boolean
>typeof strOrNumOrBool !== "string" : boolean
>typeof strOrNumOrBool : string
>strOrNumOrBool : string | number | boolean
>typeof strOrNumOrBool !== "number" : boolean
>typeof strOrNumOrBool : string
>strOrNumOrBool : 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
}
// !(typeguard1) && !(typeguard2)
if (!(typeof strOrNumOrBool === "string") && !(typeof strOrNumOrBool === "number")) {
>!(typeof strOrNumOrBool === "string") && !(typeof strOrNumOrBool === "number") : boolean
>!(typeof strOrNumOrBool === "string") : boolean
>(typeof strOrNumOrBool === "string") : boolean
>typeof strOrNumOrBool === "string" : boolean
>typeof strOrNumOrBool : string
>strOrNumOrBool : string | number | boolean
>!(typeof strOrNumOrBool === "number") : boolean
>(typeof strOrNumOrBool === "number") : boolean
>typeof strOrNumOrBool === "number" : boolean
>typeof strOrNumOrBool : string
>strOrNumOrBool : 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
}
// !(typeguard1) && simpleExpr
if (!(typeof strOrNumOrBool === "string") && numOrBool !== strOrNumOrBool) {
>!(typeof strOrNumOrBool === "string") && numOrBool !== strOrNumOrBool : boolean
>!(typeof strOrNumOrBool === "string") : boolean
>(typeof strOrNumOrBool === "string") : boolean
>typeof strOrNumOrBool === "string" : boolean
>typeof strOrNumOrBool : string
>strOrNumOrBool : string | number | boolean
>numOrBool !== strOrNumOrBool : boolean
>numOrBool : number | boolean
>strOrNumOrBool : number | boolean
numOrBool = strOrNumOrBool; // number | boolean
>numOrBool = strOrNumOrBool : number | boolean
>numOrBool : number | boolean
>strOrNumOrBool : number | boolean
}
else {
var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean
>r1 : string | number | boolean
>strOrNumOrBool : string | number | boolean
}

View file

@ -0,0 +1,53 @@
var str: string;
var bool: boolean;
var num: number;
var strOrNum: string | number;
var strOrNumOrBool: string | number | boolean;
var numOrBool: number | boolean;
// A type guard of the form !expr
// - when true, narrows the type of x by expr when false, or
// - when false, narrows the type of x by expr when true.
// !typeguard1
if (!(typeof strOrNum === "string")) {
num === strOrNum; // number
}
else {
str = strOrNum; // string
}
// !(typeguard1 || typeguard2)
if (!(typeof strOrNumOrBool === "string" || typeof strOrNumOrBool === "number")) {
bool = strOrNumOrBool; // boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
}
// !(typeguard1) || !(typeguard2)
if (!(typeof strOrNumOrBool !== "string") || !(typeof strOrNumOrBool !== "number")) {
strOrNum = strOrNumOrBool; // string | number
}
else {
bool = strOrNumOrBool; // boolean
}
// !(typeguard1 && typeguard2)
if (!(typeof strOrNumOrBool !== "string" && typeof strOrNumOrBool !== "number")) {
strOrNum = strOrNumOrBool; // string | number
}
else {
bool = strOrNumOrBool; // boolean
}
// !(typeguard1) && !(typeguard2)
if (!(typeof strOrNumOrBool === "string") && !(typeof strOrNumOrBool === "number")) {
bool = strOrNumOrBool; // boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
}
// !(typeguard1) && simpleExpr
if (!(typeof strOrNumOrBool === "string") && numOrBool !== strOrNumOrBool) {
numOrBool = strOrNumOrBool; // number | boolean
}
else {
var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean
}