doocs/leetcode · error · Error
Equal
Error message
Equal
What it means
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.
Source
Thrown at solution/2700-2799/2704.To Be Or Not To Be/Solution.ts:16
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
- 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
Example fix
// before expect(1).notToBe(1); // throws 'Equal' // after expect(1).notToBe(2); // true
Defensive patterns
Strategy: validation
Validate before calling
if (val !== notToBeVal) { expect(val).notToBe(notToBeVal); } Type guard
function canNotToBe(a: unknown, b: unknown): boolean { return a !== b; } Try / catch
try { expect(v).notToBe(e); } catch (e) { if ((e as Error).message !== 'Equal') throw e; } Prevention
- Confirm inequality intent
- Use exact 'Equal' message in toThrow
When it happens
Trigger: expect(1).notToBe(1), expect('a').notToBe('a'), expect(obj).notToBe(obj) where both sides reference the same object, or true/notToBe(true).
Common situations: 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').
Related errors
AI-assisted analysis of doocs/leetcode@f84f361dc4 (2026-08-27).
Data as JSON: /api/errors/54c19ac87a0803cd.
Report an issue: GitHub.