deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

Is assertObjectMatch too type-strict?

Open dandv opened this issue 10 months ago • 1 comments

Is your feature request related to a problem? Please describe.

If you have two typed objects, actual and expect, of the same type, assertObjectMatch doesn't let you use the expect object:

import { assertObjectMatch } from '@std/assert';

interface FirstLast {
    first: string
    last: string
}

Deno.test('assertObjectMatch', () => {
    const expected: FirstLast = {
        first: 'John',
        last: 'Doe',
    };

    const actual = expected;

    // passes
    assertObjectMatch(actual, {
        first: 'John',
        last: 'Doe',
    });

    // fails because `expected` is too typed now?
    assertObjectMatch(actual, expected);
    // Argument of type 'FirstLast' is not assignable to parameter of type 'Record<PropertyKey, unknown>'.
    // Index signature for type 'string' is missing in type 'FirstLast'.

    // casting is ugly:
    assertObjectMatch(actual, expected as unknown as Record<PropertyKey, unknown>);
});

Describe the solution you'd like

No error when passing expected objects whose type matches that of the actual object.

Describe alternatives you've considered

Asked on Discord ~a week ago, no replies yet.

dandv avatar Jan 25 '25 16:01 dandv

This looks related to this issue in typescript https://github.com/microsoft/TypeScript/issues/15300

You can fix the issue by using type for FirstLast instead of interface like:

type FirstLast = {
  first: string;
  last: string;
};

I'm not sure we should "fix" this in std.

kt3k avatar Jan 27 '25 03:01 kt3k