Skip to content

Commit

Permalink
test(equals): sufficently improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Aug 25, 2022
1 parent d9e5116 commit 24439ea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
30 changes: 25 additions & 5 deletions src/equals/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { combine, Store } from 'effector';
import { combine, Store, Unit } from 'effector';

export function equals<A, B extends A>(
a: A | Store<A>,
b: B | Store<B>,
export function equals<A, B>(
a: A extends Unit<any>
? A extends Store<infer First>
? Store<First>
: { error: `equals supports only stores and generic values` }
: A,
b: B extends Unit<any>
? B extends Store<infer Second>
? A extends Store<infer First>
? Second extends First
? Store<Second extends boolean ? boolean : Second>
: { error: 'argument b should extends a' }
: Second extends A
? Second
: { error: 'argument b should extends a' }
: { error: `equals supports only stores and generic values` }
: A extends Store<infer First>
? B extends First
? B
: { error: 'argument b should extends a' }
: B extends A
? B
: { error: 'argument b should extends a' },
): Store<boolean> {
return combine(a as Store<A>, b as Store<B>, (a, b) => a === b);
return combine(a as Store<A>, b as Store<A>, (a, b) => a === b);
}
22 changes: 15 additions & 7 deletions test-typings/equals.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { expectType } from 'tsd';
import {
Store,
createStore,
createDomain,
createEvent,
createEffect,
} from 'effector';
import { createStore, Store } from 'effector';
import { equals } from '../src/equals';

// Should receive stores with the same type
Expand Down Expand Up @@ -34,6 +28,20 @@ import { equals } from '../src/equals';
equals(createStore({ b: 2 }), createStore({ a: 1 }));
}

// Should reject stores and literals with different types
{
// @ts-expect-error
equals(createStore(true), 1);
// @ts-expect-error
equals('', createStore(1));
// @ts-expect-error
equals(createStore(''), true);
// @ts-expect-error
equals([1], createStore(['']));
// @ts-expect-error
equals(createStore({ b: 2 }), { a: 1 });
}

// Should allow to compare with literal
{
expectType<Store<boolean>>(equals(createStore(true), false));
Expand Down

0 comments on commit 24439ea

Please sign in to comment.