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

Improve Expectation["equal"] typings #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Scyfren
Copy link

@Scyfren Scyfren commented Sep 18, 2022

equal takes in unknown to allow for "negative" equals:

expect(1).never.to.equal(undefined)

but this means there is no strong typing for "positive" equals:

// stringUnion: "foo" | "bar"
expect(stringUnion).to.equal("abcde") // is allowed when it shouldn't be
// also no autocomplete when writing inside equal(...)

This MR fixes that:

// stringUnion: "foo" | "bar"
expect(stringUnion).to.equal("abcde") // type error
expect(stringUnion).to.equal("foo") // allowed
expect(stringUnion).never.to.equal("abcde") // allowed

expect(1).never.to.equal(undefined) // allowed
expect(1).never.never.to.equal(undefined) // type error
expect(1).never.never.to.equal(1) // allowed

@Scyfren
Copy link
Author

Scyfren commented Sep 18, 2022

There seems to be a bug with this fix but I'm not sure how to fix it:

expect(1).to.never.to.never.equal("abc"); // allowed when it shouldn't be
expect(1).to.never.never.equal("abc"); // type error as expected

@Scyfren
Copy link
Author

Scyfren commented Sep 18, 2022

Fixed the bug mentioned above.

expect(1).to.never.to.never.equal("abc"); // now has type error as expected
expect(1).to.never.never.equal("abc"); // type error as expected

@Scyfren
Copy link
Author

Scyfren commented Sep 18, 2022

Simplified the implementation as suggested by Dionysusnu in the discord server.
One issue is that using multiple nevers will not work as expected:

expect(1).to.equal("abc"); // type error as expected
expect(1).never.never.to.equal("abc") // allowed when it shouldn't be

but I don't see why anyone would want to use multiple nevers. The simpler fix is more readable and maintainable.

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

Successfully merging this pull request may close these issues.

1 participant