Skip to content

Instantly share code, notes, and snippets.

@adufilie
Last active January 11, 2017 18:30
Show Gist options
  • Save adufilie/0fef6f51652c352ac80856b2cf2078fa to your computer and use it in GitHub Desktop.
Save adufilie/0fef6f51652c352ac80856b2cf2078fa to your computer and use it in GitHub Desktop.
Generate double-equality versus triple-equality test code for TypeScript
// run this code in your browser console to generate TypeScript test code
function generateEqualityTests()
{
function stringify(value)
{
if (Object.is(value, undefined) || Object.is(value, NaN))
return '' + value;
return JSON.stringify(value);
}
// test cases from http://dorey.github.io/JavaScript-Equality-Table/
let values = [null, undefined, true, false, 1, 0, -1, 'true', 'false', '1', '0', '-1', '', Infinity, -Infinity, [], {}, [[]], [0], [1], NaN];
let tests = [];
for (let v1 of values)
{
for (let v2 of values)
{
let r1 = (v1 == v2);
let r2 = (v1 === v2);
if (!Object.is(r1, r2))
tests.push(`${stringify(v1)} == ${stringify(v2)}; // ${r1}`);
}
}
return tests.join('\n');
}
console.log(generateEqualityTests());
// This is the output of generateEqualityTests() and the resulting TypeScript errors.
null == undefined; // true (no error)
undefined == null; // true (no error)
true == 1; // error TS2365: Operator '==' cannot be applied to types 'true' and '1'.
true == "1"; // error TS2365: Operator '==' cannot be applied to types 'true' and '"1"'.
true == [1]; // error TS2365: Operator '==' cannot be applied to types 'boolean' and 'number[]'.
false == 0; // error TS2365: Operator '==' cannot be applied to types 'false' and '0'.
false == "0"; // error TS2365: Operator '==' cannot be applied to types 'false' and '"0"'.
false == ""; // error TS2365: Operator '==' cannot be applied to types 'false' and '""'.
false == []; // error TS2365: Operator '==' cannot be applied to types 'boolean' and 'undefined[]'.
false == [[]]; // error TS2365: Operator '==' cannot be applied to types 'boolean' and 'undefined[][]'.
false == [0]; // error TS2365: Operator '==' cannot be applied to types 'boolean' and 'number[]'.
1 == true; // error TS2365: Operator '==' cannot be applied to types '1' and 'true'.
1 == "1"; // error TS2365: Operator '==' cannot be applied to types '1' and '"1"'.
1 == [1]; // error TS2365: Operator '==' cannot be applied to types 'number' and 'number[]'.
0 == false; // error TS2365: Operator '==' cannot be applied to types '0' and 'false'.
0 == "0"; // error TS2365: Operator '==' cannot be applied to types '0' and '"0"'.
0 == ""; // error TS2365: Operator '==' cannot be applied to types '0' and '""'.
0 == []; // error TS2365: Operator '==' cannot be applied to types 'number' and 'undefined[]'.
0 == [[]]; // error TS2365: Operator '==' cannot be applied to types 'number' and 'undefined[][]'.
0 == [0]; // error TS2365: Operator '==' cannot be applied to types 'number' and 'number[]'.
-1 == "-1"; // error TS2365: Operator '==' cannot be applied to types '-1' and '"-1"'.
"1" == true; // error TS2365: Operator '==' cannot be applied to types '"1"' and 'true'.
"1" == 1; // error TS2365: Operator '==' cannot be applied to types '"1"' and '1'.
"1" == [1]; // error TS2365: Operator '==' cannot be applied to types 'string' and 'number[]'.
"0" == false; // error TS2365: Operator '==' cannot be applied to types '"0"' and 'false'.
"0" == 0; // error TS2365: Operator '==' cannot be applied to types '"0"' and '0'.
"0" == [0]; // error TS2365: Operator '==' cannot be applied to types 'string' and 'number[]'.
"-1" == -1; // error TS2365: Operator '==' cannot be applied to types '"-1"' and '-1'.
"" == false; // error TS2365: Operator '==' cannot be applied to types '""' and 'false'.
"" == 0; // error TS2365: Operator '==' cannot be applied to types '""' and '0'.
"" == []; // error TS2365: Operator '==' cannot be applied to types 'string' and 'undefined[]'.
"" == [[]]; // error TS2365: Operator '==' cannot be applied to types 'string' and 'undefined[][]'.
[] == false; // error TS2365: Operator '==' cannot be applied to types 'undefined[]' and 'boolean'.
[] == 0; // error TS2365: Operator '==' cannot be applied to types 'undefined[]' and 'number'.
[] == ""; // error TS2365: Operator '==' cannot be applied to types 'undefined[]' and 'string'.
[[]] == false; // error TS2365: Operator '==' cannot be applied to types 'undefined[][]' and 'boolean'.
[[]] == 0; // error TS2365: Operator '==' cannot be applied to types 'undefined[][]' and 'number'.
[[]] == ""; // error TS2365: Operator '==' cannot be applied to types 'undefined[][]' and 'string'.
[0] == false; // error TS2365: Operator '==' cannot be applied to types 'number[]' and 'boolean'.
[0] == 0; // error TS2365: Operator '==' cannot be applied to types 'number[]' and 'number'.
[0] == "0"; // error TS2365: Operator '==' cannot be applied to types 'number[]' and 'string'.
[1] == true; // error TS2365: Operator '==' cannot be applied to types 'number[]' and 'boolean'.
[1] == 1; // error TS2365: Operator '==' cannot be applied to types 'number[]' and 'number'.
[1] == "1"; // error TS2365: Operator '==' cannot be applied to types 'number[]' and 'string'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment