doocs/leetcode · error · Error
Not Equal
Error message
Not Equal
What it means
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.
Source
Thrown at solution/2700-2799/2704.To Be Or Not To Be/README_EN.md:73
<!-- solution:start -->
### Solution 1
<!-- tabs:start -->
#### TypeScript
```ts
type ToBeOrNotToBe = {
toBe: (val: any) => boolean;
notToBe: (val: any) => boolean;
};
function expect(val: any): ToBeOrNotToBe {
return {
toBe: (toBeVal: any) => {
if (val !== toBeVal) {
throw new Error('Not Equal');
}
return true;
},
notToBe: (notToBeVal: any) => {
if (val === notToBeVal) {
throw new Error('Equal');
}
return true;
},
};
}
/**
* expect(5).toBe(5); // true
* expect(5).notToBe(5); // throws "Equal"
*/
```
View on GitHub (pinned to f84f361dc4)
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
Example fix
// before
expect(JSON.parse('"5"')).toBe(5); // throws
// after
expect(Number(JSON.parse('"5"'))).toBe(5); // true Defensive patterns
Strategy: validation
Validate before calling
if (Number(val) === Number(toBeVal) && typeof val === typeof toBeVal) { /* safe */ } Type guard
function sameTypeAndEqual(a: unknown, b: unknown): boolean { return typeof a === typeof b && a === b; } Try / catch
try { expect(v).toBe(e); } catch (e) { if ((e as Error).message !== 'Not Equal') throw e; } Prevention
- Avoid any typing; use literals/primitives
- Deep-compare objects
When it happens
Trigger: expect(5).toBe('5'), expect([1,2]).toBe([1,2]), expect(undefined).toBe(null), expect(NaN).toBe(NaN).
Common situations: 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.
Related errors
AI-assisted analysis of doocs/leetcode@f84f361dc4 (2026-08-27).
Data as JSON: /api/errors/539e701228956ed5.
Report an issue: GitHub.