{"record":{"id":"1866d3a819b5efc3","repo":"doocs/leetcode","slug":"not-equal-1866d3","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/Solution.ts","lineNumber":10,"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 version of the LeetCode 2704 expect helper: toBe compares val with toBeVal using strict equality (!==) and throws Error('Not Equal') on any strict mismatch. Because both values are typed any, the compiler will not warn about comparing mismatched types, so '5' vs 5 or distinct object references silently reach the runtime check.","triggerScenarios":"expect(5).toBe('5'), expect(NaN).toBe(NaN) (NaN is never strictly equal to itself), expect({a:1}).toBe({a:1}) (different references), or any strict-equality failure between val and the argument.","commonSituations":"Assuming any-typed parameters give compile-time protection; expecting deep equality from toBe; porting Jest's toBe semantics (which pretty-prints diffs) into this minimal API; comparing boxed numbers/strings.","solutions":["Verify strict equality is what you meant; coerce types before comparing (Number(x) vs Number(y))","For objects/arrays, compare via JSON.stringify or a deep-equal function, not toBe","Remember NaN: use Number.isNaN checks before toBe(NaN)","Type the parameters (val: number | string) so the compiler flags nonsense comparisons"],"exampleFix":"// before\nexpect('5' as any).toBe(5); // throws 'Not Equal'\n\n// after\nexpect(Number('5')).toBe(5); // true","handlingStrategy":"validation","validationCode":"if (typeof val === typeof toBeVal && val === toBeVal) { /* safe */ }","typeGuard":"function canToBe(a: unknown, b: unknown): boolean { return a === b; }","tryCatchPattern":"try { expect(v).toBe(e); } catch (e) { if (!(e instanceof Error) || e.message !== 'Not Equal') throw e; }","preventionTips":["Type parameters instead of any to catch mismatches at compile time","Coerce before compare","Deep-compare structures"],"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"}