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

Fix input parameter type for notEquals/notDeepEquals #71

Open
soulofmischief opened this issue Jul 8, 2024 · 1 comment
Open

Fix input parameter type for notEquals/notDeepEquals #71

soulofmischief opened this issue Jul 8, 2024 · 1 comment

Comments

@soulofmischief
Copy link

soulofmischief commented Jul 8, 2024

Currently, notEquals/notDeepEquals expect type T for the input parameter. They should actually expect anything except for T, and thus throw a TypeScript error when used correctly in a TypeScript environment.

The simple solution is just to do a generic like such:

notEquals<U>(value: U): AssertionDescriber;
notDeepEquals<U>(value: U): AssertionDescriber;

We can also explicitly exclude T somehow like such:

soulofmischief/DefinitelyTyped@d0268d6

but this technique would be overly restrictive for notEquals and I'm unsure how to make it less restrictive without built-in language support. It would additionally require extra boilerplate to handle primitive types, which it currently fails on.

If #47 moves forward then notDeepEquals doesn't need special handling anyway and the simple generic solution proposed above might be the most straightforward fix.

@dead-claudia
Copy link
Member

Honestly, just changing the expected value's type from T to unknown for all equality operators would be better.

Note that, if/when you do that, you'll need a private fake field hack to ensure the type gets passed through for o(1).throws("error") to get correctly rejected.

// Do not export
declare const wrapped: unique symbol

namespace o {
    // ...

    interface Assertion<T> {
        // Set to a function to preserve the correct variance for assignability
        [wrapped]: (value: T) => any

        // Existing methods...
    }
}

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