MithrilJS / ospec

Noiseless testing framework
MIT License
48 stars 13 forks source link

Fix input parameter type for notEquals/notDeepEquals #71

Open soulofmischief opened 2 months ago

soulofmischief commented 2 months ago

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:

https://github.com/soulofmischief/DefinitelyTyped/commit/d0268d6a259ce61e4e0370ce1c69e94841b672e5

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 https://github.com/MithrilJS/ospec/issues/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 commented 2 months ago

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...
    }
}