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

Loosen type constraints for deepEqual assertion #2969

Merged
merged 1 commit into from Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions test-d/deep-equal.ts
@@ -0,0 +1,30 @@
import {expectType} from 'tsd';

import test from '..';

test('actual extends expected', t => {
type Expected = {foo: [1, 2, 3]};
const expected: Expected = {foo: [1, 2, 3]};
const actual = {foo: [1, 2, 3]};
if (t.deepEqual(actual, expected)) {
expectType<Expected>(actual);
}
});

test('expected extends actual', t => {
type Expected = {foo: Array<number | string>};
type Actual = {foo: number[]};
const expected: Expected = {foo: [1, 2, 3]};
const actual: Actual = {foo: [1, 2, 3]};
if (t.deepEqual(actual, expected)) {
expectType<Actual>(expected);
}
});

test('neither extends the each other', t => {
type Expected = {readonly foo: readonly [1, 2, 3]};
type Actual = {foo: number[]};
const expected: Expected = {foo: [1, 2, 3]};
const actual: Actual = {foo: [1, 2, 3]};
t.deepEqual(actual, expected);
});
12 changes: 12 additions & 0 deletions types/assertions.d.ts
Expand Up @@ -138,6 +138,18 @@ export interface DeepEqualAssertion {
*/
<Actual, Expected extends Actual>(actual: Actual, expected: Expected, message?: string): actual is Expected;

/**
* Assert that `actual` is [deeply equal](https://github.com/concordancejs/concordance#comparison-details) to
* `expected`, returning a boolean indicating whether the assertion passed.
*/
<Actual extends Expected, Expected>(actual: Actual, expected: Expected, message?: string): expected is Actual;
RebeccaStevens marked this conversation as resolved.
Show resolved Hide resolved

/**
* Assert that `actual` is [deeply equal](https://github.com/concordancejs/concordance#comparison-details) to
* `expected`, returning a boolean indicating whether the assertion passed.
*/
<Actual, Expected>(actual: Actual, expected: Expected, message?: string): boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this overload? Wouldn't the assertion always fail if neither extend one another?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If part of Expected extends part of Actual, but another part Expected is a super type of Actual, then neither extends the other but both my have the same value.

For example, imagine that one of the properties on Expected is typed as readonly, while the corresponding property on Actual isn't. And then for another property the two share, the readonlyness is declared the other way around.


/** Skip this assertion. */
skip(actual: any, expected: any, message?: string): void;
}
Expand Down