deno_std
deno_std copied to clipboard
Is assertObjectMatch too type-strict?
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.
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.