diff --git a/tests/baselines/reference/typeGuardOfFormNotExpr.js b/tests/baselines/reference/typeGuardOfFormNotExpr.js new file mode 100644 index 0000000000..62a1b3ff94 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormNotExpr.js @@ -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 +} diff --git a/tests/baselines/reference/typeGuardOfFormNotExpr.types b/tests/baselines/reference/typeGuardOfFormNotExpr.types new file mode 100644 index 0000000000..22d1d71f41 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormNotExpr.types @@ -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 +} diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormNotExpr.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormNotExpr.ts new file mode 100644 index 0000000000..c9a2cb5fc0 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormNotExpr.ts @@ -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 +} \ No newline at end of file