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

  1. Confirm the values really should differ; if they are equal, switch to toBe
  2. Use distinct values or copies (structuredClone) when you need reference inequality
  3. For content-based checks, compare serialized forms instead of notToBe
  4. 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

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.