{"record":{"id":"ba0da6a9be571434","repo":"doocs/leetcode","slug":"not-equal-ba0da6","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.md","lineNumber":74,"sourceCode":"<!-- solution:start -->\n\n### 方法一\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":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/doocs/leetcode/blob/f84f361dc403a65c4c031f6e0774297cc377f00a/solution/2700-2799/2704.To Be Or Not To Be/README.md#L56-L92","documentation":"README (TypeScript tab) copy of the LeetCode 2704 expect helper: toBe throws Error('Not Equal') when val !== toBeVal under strict equality. The README documents the exact behavior the judge tests, including that '5' and 5 are not equal and that object identity, not contents, is compared.","triggerScenarios":"expect('5').toBe(5), expect(null).toBe(undefined), expect({}).toBe({}), expect(NaN).toBe(NaN) — all strict-equality failures.","commonSituations":"Readers replicating the example calls; misunderstanding === vs ==; expecting Jest-style deep equality from toBe; passing values parsed from JSON where types shift (string 'true' vs boolean).","solutions":["Compare like types: coerce with Number()/String()/Boolean() before toBe","Use deep comparison for objects: JSON.stringify(a) === JSON.stringify(b)","Handle NaN separately with Number.isisNaN checks","Assert the throw itself with toThrow('Not Equal') when demonstrating the API"],"exampleFix":"// before\nexpect('true').toBe(true); // throws 'Not Equal'\n\n// after\nexpect('true' === 'true').toBe(true); // true","handlingStrategy":"validation","validationCode":"if (typeof val === typeof toBeVal && val === toBeVal) { /* toBe passes */ }","typeGuard":"const strictEqual = (a: unknown, b: unknown): boolean => a === b;","tryCatchPattern":"try { expect(v).toBe(e); } catch (e) { if ((e as Error).message !== 'Not Equal') throw e; }","preventionTips":["Coerce types first","Deep-compare via JSON.stringify for structures"],"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"}