Primitive type guards are now order independent

This commit is contained in:
Nathan Shively-Sanders
2016-06-08 15:56:25 -07:00
parent 7c1b28f2cb
commit 11377f9fd3
4 changed files with 275 additions and 0 deletions
@@ -0,0 +1,71 @@
//// [typeGuardOfFormTypeOfIsOrderIndependent.ts]
var strOrNum: string | number;
var strOrBool: string | boolean;
var strOrFunc: string | (() => void);
var numOrBool: number | boolean
var str: string;
var num: number;
var bool: boolean;
var func: () => void;
if ("string" === typeof strOrNum) {
// if (typeof strOrNum === "string") {
str = strOrNum;
}
else {
num = strOrNum;
}
if ("function" === typeof strOrFunc) {
func = strOrFunc;
}
else {
str = strOrFunc;
}
if ("number" === typeof numOrBool) {
num = numOrBool;
}
else {
bool = numOrBool;
}
if ("boolean" === typeof strOrBool) {
bool = strOrBool;
}
else {
str = strOrBool;
}
//// [typeGuardOfFormTypeOfIsOrderIndependent.js]
var strOrNum;
var strOrBool;
var strOrFunc;
var numOrBool;
var str;
var num;
var bool;
var func;
if ("string" === typeof strOrNum) {
// if (typeof strOrNum === "string") {
str = strOrNum;
}
else {
num = strOrNum;
}
if ("function" === typeof strOrFunc) {
func = strOrFunc;
}
else {
str = strOrFunc;
}
if ("number" === typeof numOrBool) {
num = numOrBool;
}
else {
bool = numOrBool;
}
if ("boolean" === typeof strOrBool) {
bool = strOrBool;
}
else {
str = strOrBool;
}
@@ -0,0 +1,75 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfIsOrderIndependent.ts ===
var strOrNum: string | number;
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 0, 3))
var strOrBool: string | boolean;
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 1, 3))
var strOrFunc: string | (() => void);
>strOrFunc : Symbol(strOrFunc, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 2, 3))
var numOrBool: number | boolean
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 3, 3))
var str: string;
>str : Symbol(str, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 4, 3))
var num: number;
>num : Symbol(num, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 5, 3))
var bool: boolean;
>bool : Symbol(bool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 6, 3))
var func: () => void;
>func : Symbol(func, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 7, 3))
if ("string" === typeof strOrNum) {
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 0, 3))
// if (typeof strOrNum === "string") {
str = strOrNum;
>str : Symbol(str, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 4, 3))
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 0, 3))
}
else {
num = strOrNum;
>num : Symbol(num, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 5, 3))
>strOrNum : Symbol(strOrNum, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 0, 3))
}
if ("function" === typeof strOrFunc) {
>strOrFunc : Symbol(strOrFunc, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 2, 3))
func = strOrFunc;
>func : Symbol(func, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 7, 3))
>strOrFunc : Symbol(strOrFunc, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 2, 3))
}
else {
str = strOrFunc;
>str : Symbol(str, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 4, 3))
>strOrFunc : Symbol(strOrFunc, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 2, 3))
}
if ("number" === typeof numOrBool) {
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 3, 3))
num = numOrBool;
>num : Symbol(num, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 5, 3))
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 3, 3))
}
else {
bool = numOrBool;
>bool : Symbol(bool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 6, 3))
>numOrBool : Symbol(numOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 3, 3))
}
if ("boolean" === typeof strOrBool) {
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 1, 3))
bool = strOrBool;
>bool : Symbol(bool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 6, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 1, 3))
}
else {
str = strOrBool;
>str : Symbol(str, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 4, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardOfFormTypeOfIsOrderIndependent.ts, 1, 3))
}
@@ -0,0 +1,95 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfIsOrderIndependent.ts ===
var strOrNum: string | number;
>strOrNum : string | number
var strOrBool: string | boolean;
>strOrBool : string | boolean
var strOrFunc: string | (() => void);
>strOrFunc : string | (() => void)
var numOrBool: number | boolean
>numOrBool : number | boolean
var str: string;
>str : string
var num: number;
>num : number
var bool: boolean;
>bool : boolean
var func: () => void;
>func : () => void
if ("string" === typeof strOrNum) {
>"string" === typeof strOrNum : boolean
>"string" : string
>typeof strOrNum : string
>strOrNum : string | number
// if (typeof strOrNum === "string") {
str = strOrNum;
>str = strOrNum : string
>str : string
>strOrNum : string
}
else {
num = strOrNum;
>num = strOrNum : number
>num : number
>strOrNum : number
}
if ("function" === typeof strOrFunc) {
>"function" === typeof strOrFunc : boolean
>"function" : string
>typeof strOrFunc : string
>strOrFunc : string | (() => void)
func = strOrFunc;
>func = strOrFunc : () => void
>func : () => void
>strOrFunc : () => void
}
else {
str = strOrFunc;
>str = strOrFunc : string
>str : string
>strOrFunc : string
}
if ("number" === typeof numOrBool) {
>"number" === typeof numOrBool : boolean
>"number" : string
>typeof numOrBool : string
>numOrBool : number | boolean
num = numOrBool;
>num = numOrBool : number
>num : number
>numOrBool : number
}
else {
bool = numOrBool;
>bool = numOrBool : boolean
>bool : boolean
>numOrBool : boolean
}
if ("boolean" === typeof strOrBool) {
>"boolean" === typeof strOrBool : boolean
>"boolean" : string
>typeof strOrBool : string
>strOrBool : string | boolean
bool = strOrBool;
>bool = strOrBool : boolean
>bool : boolean
>strOrBool : boolean
}
else {
str = strOrBool;
>str = strOrBool : string
>str : string
>strOrBool : string
}
@@ -0,0 +1,34 @@
var strOrNum: string | number;
var strOrBool: string | boolean;
var strOrFunc: string | (() => void);
var numOrBool: number | boolean
var str: string;
var num: number;
var bool: boolean;
var func: () => void;
if ("string" === typeof strOrNum) {
// if (typeof strOrNum === "string") {
str = strOrNum;
}
else {
num = strOrNum;
}
if ("function" === typeof strOrFunc) {
func = strOrFunc;
}
else {
str = strOrFunc;
}
if ("number" === typeof numOrBool) {
num = numOrBool;
}
else {
bool = numOrBool;
}
if ("boolean" === typeof strOrBool) {
bool = strOrBool;
}
else {
str = strOrBool;
}