doocs/leetcode · error · Error

Equal

Error message

Equal

What it means

English README TypeScript tab, notToBe branch of LeetCode 2704: throws Error('Equal') when val === notToBeVal. The inverted assertion only returns true for strictly different values; identical primitives or the same object reference trip the guard.

Source

Thrown at solution/2700-2799/2704.To Be Or Not To Be/README_EN.md:79

#### 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"
 */
```

#### JavaScript

```js
/**
 * @param {string} val
 * @return {Object}

View on GitHub (pinned to f84f361dc4)

Solutions

  1. Verify inequality is actually expected; otherwise switch to toBe
  2. For object inequality, pass distinct instances ({}).notToBe({}) works
  3. Use the exact 'Equal' message in toThrow checks
  4. Re-read the API: notToBe is the negation of toBe

Example fix

// before
expect('a').notToBe('a'); // throws 'Equal'

// after
expect('a').notToBe('b'); // true
Defensive patterns

Strategy: validation

Validate before calling

if (val !== notToBeVal) { expect(val).notToBe(notToBeVal); }

Type guard

function differs(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(true).notToBe(true), expect('a').notToBe('a'), expect(x).notToBe(x) with the same variable on both sides.

Common situations: Adapting the sample tests; asserting the wrong message text; assuming two separately written literals would be distinguishable references (primitives are compared by value).

Related errors


AI-assisted analysis of doocs/leetcode@f84f361dc4 (2026-08-27). Data as JSON: /api/errors/45298d5c03f35ff8. Report an issue: GitHub.