{"record":{"id":"539e701228956ed5","repo":"doocs/leetcode","slug":"not-equal-539e70","errorCode":null,"errorMessage":"Not Equal","messagePattern":"Not Equal","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"solution/2700-2799/2704.To Be Or Not To Be/README_EN.md","lineNumber":73,"sourceCode":"<!-- solution:start -->\n\n### Solution 1\n\n<!-- tabs:start -->\n\n#### TypeScript\n\n```ts\ntype 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```\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/doocs/leetcode/blob/f84f361dc403a65c4c031f6e0774297cc377f00a/solution/2700-2799/2704.To Be Or Not To Be/README_EN.md#L55-L91","documentation":"English README TypeScript tab of LeetCode 2704: toBe(toBeVal) throws Error('Not Equal') on strict-equality failure between the constructor-captured val and the argument. It mirrors the Solution.ts behavior and documents that identity (for objects) and exact type+value (for primitives) are required.","triggerScenarios":"expect(5).toBe('5'), expect([1,2]).toBe([1,2]), expect(undefined).toBe(null), expect(NaN).toBe(NaN).","commonSituations":"Running README samples in a playground; translating from loosely-typed comparison habits; expecting array/object deep equality; values flowing in from JSON.parse keeping string types where numbers were assumed.","solutions":["Normalize types before comparing","Use serialized deep comparison for structures","Special-case NaN with Number.isNaN","Use toThrow('Not Equal') in tests that intentionally demonstrate failure"],"exampleFix":"// before\nexpect(JSON.parse('\"5\"')).toBe(5); // throws\n\n// after\nexpect(Number(JSON.parse('\"5\"'))).toBe(5); // true","handlingStrategy":"validation","validationCode":"if (Number(val) === Number(toBeVal) && typeof val === typeof toBeVal) { /* safe */ }","typeGuard":"function sameTypeAndEqual(a: unknown, b: unknown): boolean { return typeof a === typeof b && a === b; }","tryCatchPattern":"try { expect(v).toBe(e); } catch (e) { if ((e as Error).message !== 'Not Equal') throw e; }","preventionTips":["Avoid any typing; use literals/primitives","Deep-compare objects"],"tags":["typescript","assertion","equality","documentation"],"backgroundTag":"assertion-equality-failure","analyzedSha":"f84f361dc403a65c4c031f6e0774297cc377f00a","analyzedAt":"2026-08-27T04:29:31.522Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}