Skip to content

[std/testing] Function signature of assertObjectEquals() does not accept interfaces #741

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

Closed
Seally opened this issue Feb 18, 2021 · 3 comments

Comments

@Seally
Copy link

Seally commented Feb 18, 2021

The type Record<PropertyKey, unknown> used in assertObjectEquals() does not accept interface types as an argument. This is due to an intentional quirk in how TypeScript operates with interfaces. With that said, I do not believe this assertion function should disallow inputs that uses types declared with the interface keyword.

import { assertObjectMatch } from "https://deno.land/[email protected]/testing/asserts.ts";

interface NumberToken {
    type: "number";
    value: number;
}

const token: NumberToken = {
    type: "number",
    value: 15
};

assertObjectMatch(token, { type: "number" }); // doesn't work

type TNumberToken = {
    type: "number";
    value: number;
};

const typeToken: TNumberToken = {
    type: "number",
    value: 15
};

assertObjectMatch(typeToken, { type: "number" }); // works

I'm not completely sure what to suggest changing this to, but the most minimal change is to use Record<PropertyKey, any> instead, which seems to work for this case and confirmed to not be a bug from the TypeScript team.

Here are some test inputs for the Record<PropertyKey, any> signature:

// Continued from previous block
function foo(input: Record<PropertyKey, any>) {}

foo(token); // pass
foo(typeToken); // pass
foo(foo); // pass
foo(""); // fail
foo(15); // fail

Relevant issues:

@cola119
Copy link

cola119 commented Feb 24, 2021

I'm facing this issue too. May I make the p-r to change the props type Record<PropertyKey, unknown> to Record<PropertyKey, any>? I think this change is trivial and there don't seem to be any unexpected problems.

@getspooky
Copy link
Contributor

I fixed #763

wperron added a commit that referenced this issue Apr 5, 2021
@wperron
Copy link
Contributor

wperron commented Apr 5, 2021

Fixed in #763

@wperron wperron closed this as completed Apr 5, 2021
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

4 participants