doocs/leetcode · error · Error

Not Equal

Error message

Not Equal

What it means

README (TypeScript tab) copy of the LeetCode 2704 expect helper: toBe throws Error('Not Equal') when val !== toBeVal under strict equality. The README documents the exact behavior the judge tests, including that '5' and 5 are not equal and that object identity, not contents, is compared.

Source

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

<!-- solution:start -->

### 方法一

<!-- 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

  1. Compare like types: coerce with Number()/String()/Boolean() before toBe
  2. Use deep comparison for objects: JSON.stringify(a) === JSON.stringify(b)
  3. Handle NaN separately with Number.isisNaN checks
  4. Assert the throw itself with toThrow('Not Equal') when demonstrating the API

Example fix

// before
expect('true').toBe(true); // throws 'Not Equal'

// after
expect('true' === 'true').toBe(true); // true
Defensive patterns

Strategy: validation

Validate before calling

if (typeof val === typeof toBeVal && val === toBeVal) { /* toBe passes */ }

Type guard

const strictEqual = (a: unknown, b: unknown): boolean => a === b;

Try / catch

try { expect(v).toBe(e); } catch (e) { if ((e as Error).message !== 'Not Equal') throw e; }

Prevention

When it happens

Trigger: expect('5').toBe(5), expect(null).toBe(undefined), expect({}).toBe({}), expect(NaN).toBe(NaN) — all strict-equality failures.

Common situations: Readers replicating the example calls; misunderstanding === vs ==; expecting Jest-style deep equality from toBe; passing values parsed from JSON where types shift (string 'true' vs boolean).

Related errors


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