Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is assertObjectMatch too type-strict? #6367

Open
dandv opened this issue Jan 25, 2025 · 1 comment
Open

Is assertObjectMatch too type-strict? #6367

dandv opened this issue Jan 25, 2025 · 1 comment

Comments

@dandv
Copy link
Contributor

dandv commented Jan 25, 2025

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.

@kt3k
Copy link
Member

kt3k commented Jan 27, 2025

This looks related to this issue in typescript microsoft/TypeScript#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants