doocs/leetcode · error · Error

Equal

Error message

Equal

What it means

README TypeScript tab for LeetCode 2704: notToBe throws Error('Equal') when val === notToBeVal. It is the complement assertion — it only succeeds when the two any-typed values are strictly different, and it fires when they turn out to be the same.

Source

Thrown at solution/2700-2799/2704.To Be Or Not To Be/README.md:80

#### 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 the pair actually differs; if equal, use toBe
  2. Create separate objects when you need reference inequality
  3. When asserting the throw, match the exact message 'Equal'
  4. Remember only toBe throws 'Not Equal' — notToBe throws 'Equal'

Example fix

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

// after
expect(1).notToBe('1'); // true (strict inequality)
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

const strictNotEqual = (a: unknown, b: unknown): boolean => 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(undefined).notToBe(undefined), expect(sameRef).notToBe(sameRef).

Common situations: Copying README examples and flipping the wrong assertion; believing two equal-looking values are distinct (e.g. interned strings in JS); testing the utility with toThrow and passing the wrong message string ('Not Equal' vs 'Equal').

Related errors


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