Skip to content

Commit

Permalink
Loosen type constraints for deepEqual assertion
Browse files Browse the repository at this point in the history
Allow actual to extend expected, or for the types to not extend each other at all.
  • Loading branch information
RebeccaStevens committed Mar 6, 2022
1 parent b5f934d commit 9f797b0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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;

/**
* 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;

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

0 comments on commit 9f797b0

Please sign in to comment.