dandvD
Denoβ€’12mo ago
dandv

Is assertObjectMatch too type-strict?

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>);
});


What am I doing wrong here?
Was this page helpful?