From 11377f9fd342dc21012e9cff1d60d0a2226f30e6 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 8 Jun 2016 15:56:25 -0700 Subject: [PATCH] Primitive type guards are now order independent --- ...typeGuardOfFormTypeOfIsOrderIndependent.js | 71 ++++++++++++++ ...uardOfFormTypeOfIsOrderIndependent.symbols | 75 +++++++++++++++ ...eGuardOfFormTypeOfIsOrderIndependent.types | 95 +++++++++++++++++++ ...typeGuardOfFormTypeOfIsOrderIndependent.ts | 34 +++++++ 4 files changed, 275 insertions(+) create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.js create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.symbols create mode 100644 tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfIsOrderIndependent.ts diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.js b/tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.js new file mode 100644 index 00000000000..5ea7932e89a --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.js @@ -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; +} diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.symbols b/tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.symbols new file mode 100644 index 00000000000..d75774eba45 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.symbols @@ -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)) +} + diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.types b/tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.types new file mode 100644 index 00000000000..592d147f386 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormTypeOfIsOrderIndependent.types @@ -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 +} + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfIsOrderIndependent.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfIsOrderIndependent.ts new file mode 100644 index 00000000000..79fcf49e245 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfIsOrderIndependent.ts @@ -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; +}