{"record":{"id":"54c19ac87a0803cd","repo":"doocs/leetcode","slug":"equal-54c19a","errorCode":null,"errorMessage":"Equal","messagePattern":"Equal","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"solution/2700-2799/2704.To Be Or Not To Be/Solution.ts","lineNumber":16,"sourceCode":"type ToBeOrNotToBe = {\n    toBe: (val: any) => boolean;\n    notToBe: (val: any) => boolean;\n};\n\nfunction expect(val: any): ToBeOrNotToBe {\n    return {\n        toBe: (toBeVal: any) => {\n            if (val !== toBeVal) {\n                throw new Error('Not Equal');\n            }\n            return true;\n        },\n        notToBe: (notToBeVal: any) => {\n            if (val === notToBeVal) {\n                throw new Error('Equal');\n            }\n            return true;\n        },\n    };\n}\n\n/**\n * expect(5).toBe(5); // true\n * expect(5).notToBe(5); // throws \"Equal\"\n */\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/doocs/leetcode/blob/f84f361dc403a65c4c031f6e0774297cc377f00a/solution/2700-2799/2704.To Be Or Not To Be/Solution.ts#L1-L27","documentation":"TypeScript variant of notToBe from LeetCode 2704: it throws Error('Equal') when val === notToBeVal, i.e. when an assertion of inequality fails. The any typing again means no compile-time guard, so identical primitives or shared references reach the runtime check.","triggerScenarios":"expect(1).notToBe(1), expect('a').notToBe('a'), expect(obj).notToBe(obj) where both sides reference the same object, or true/notToBe(true).","commonSituations":"Inverting assertion intent (using notToBe where the values genuinely match); assuming two separately-built objects with equal contents would be 'not equal' (they are, by reference, but the same reference is not); testing the API surface itself with toThrow('Equal').","solutions":["Confirm the values really should differ; if they are equal, switch to toBe","Use distinct values or copies (structuredClone) when you need reference inequality","For content-based checks, compare serialized forms instead of notToBe","Assert the throw explicitly when unit-testing this helper"],"exampleFix":"// before\nexpect(1).notToBe(1); // throws 'Equal'\n\n// after\nexpect(1).notToBe(2); // true","handlingStrategy":"validation","validationCode":"if (val !== notToBeVal) { expect(val).notToBe(notToBeVal); }","typeGuard":"function canNotToBe(a: unknown, b: unknown): boolean { return a !== b; }","tryCatchPattern":"try { expect(v).notToBe(e); } catch (e) { if ((e as Error).message !== 'Equal') throw e; }","preventionTips":["Confirm inequality intent","Use exact 'Equal' message in toThrow"],"tags":["typescript","assertion","equality","leetcode"],"backgroundTag":"assertion-equality-failure","analyzedSha":"f84f361dc403a65c4c031f6e0774297cc377f00a","analyzedAt":"2026-08-27T04:29:31.522Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}