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

Enable Epic#onMany to be called with many arguments #86

Merged
merged 4 commits into from
Sep 7, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/typeless-form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"rimraf": "^3.0.2",
"rxjs": "^6.3.3",
"tslint": "6.0.0",
"typescript": "3.8.2"
"typescript": "^3.9.6"
},
"resolutions": {
"@types/react": "^16.8.12"
Expand Down
2 changes: 1 addition & 1 deletion packages/typeless-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"rimraf": "^3.0.2",
"rxjs": "^6.3.3",
"tslint": "6.0.0",
"typescript": "3.8.2"
"typescript": "^3.9.6"
},
"resolutions": {
"@types/react": "^16.8.12"
Expand Down
42 changes: 42 additions & 0 deletions packages/typeless/__tests__/type/Epic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createModule, ActionMap } from '../../src/createModule';
import { Epic } from '../../src/Epic';
import { TT } from './TypeTester';

function createAction<T extends ActionMap>(acts: T) {
return createModule(Symbol('test')).withActions(acts)[1];
}
TT.describe('onMany', () => {
const m1 = createAction({ foo: (num: number) => ({ payload: { num } }) });
const m2 = createAction({ foo: (boo: boolean) => ({ payload: { boo } }) });
const m3 = createAction({ foo: () => ({ payload: 'bar' }) });
const m4 = createAction({ foo: () => ({ payload: Symbol('baz') }) });
const m5 = createAction({ foo: (str: string) => ({ payload: { str } }) });
const m6 = createAction({ foo: () => ({ payload: ['array'] }) });

TT.describe('should error called with', () => {
TT.it('empty array', () => {
// @ts-expect-error
new Epic().onMany([], () => null);
});
TT.it('has 1 item array', () => {
// @ts-expect-error
new Epic().onMany([m1.foo], () => null);
});
});
TT.describe("should infer callback's arguments", () => {
TT.it('called with many arguments', () => {
new Epic().onMany([m1.foo, m2.foo, m3.foo, m4.foo, m5.foo, m6.foo], p => {
type Expected =
| { num: number }
| { boo: boolean }
| string
| symbol
| { str: string }
| string[];
TT.assert<TT.Eq<typeof p, Expected>>();

return null;
});
});
});
});
4 changes: 4 additions & 0 deletions packages/typeless/__tests__/type/useMappedState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ TT.describe('useMappedState', () => {
const getCountState = createTestModule<{ count: number }>();
const getTextState = createTestModule<{ text: string }>();

TT.it('should error with empty array', () => {
// @ts-expect-error
useMappedState([], () => null);
});
TT.it("should infer callback's arguments type", () => {
useMappedState([getCountState, getTextState], (c, t) => {
TT.assert<TT.Eq<typeof c, { count: number }>>();
Expand Down
2 changes: 1 addition & 1 deletion packages/typeless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"shallow-equal": "^1.2.1",
"ts-jest": "^25.5.0",
"tslint": "6.0.0",
"typescript": "3.8.2"
"typescript": "^3.9.6"
},
"peerDependencies": {
"react": "^16.8.2",
Expand Down
24 changes: 1 addition & 23 deletions packages/typeless/src/Epic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,7 @@ export class Epic {
on<TAC extends AC>(ac: TAC, handler: EpicHandler<TAC>) {
return this.add(ac, handler);
}
onMany<TAC extends AC, TAC2 extends AC>(
ac: [TAC, TAC2],
handler: EpicHandler<TAC | TAC2>
): this;
onMany<TAC extends AC, TAC2 extends AC, TAC3 extends AC>(
ac: [TAC, TAC2, TAC3],
handler: EpicHandler<TAC | TAC2 | TAC3>
): this;
onMany<TAC extends AC, TAC2 extends AC, TAC3 extends AC, TAC4 extends AC>(
ac: [TAC, TAC2, TAC3, TAC4],
handler: EpicHandler<TAC | TAC2 | TAC3 | TAC4>
): this;
onMany<
TAC extends AC,
TAC2 extends AC,
TAC3 extends AC,
TAC4 extends AC,
TAC5 extends AC
>(
ac: [TAC, TAC2, TAC3, TAC4, TAC5],
handler: EpicHandler<TAC | TAC2 | TAC3 | TAC4 | TAC5>
): this;
onMany(ac: AC[], handler: EpicHandler<AC>) {
onMany<T extends [AC, AC, ...AC[]]>(ac: T, handler: EpicHandler<T[number]>) {
return this.add(ac, handler);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/typeless/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export interface StateGetter<T> {
useState(): T;
}

export type TupleOfStateGetter = [] | [StateGetter<any>, ...StateGetter<any>[]];
export type TupleOfStateGetter = [StateGetter<any>, ...StateGetter<any>[]];

export type EqualityFn<T> = (a: T, b: T) => boolean;
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7570,10 +7570,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@3.8.2:
version "3.8.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.2.tgz#91d6868aaead7da74f493c553aeff76c0c0b1d5a"
integrity sha512-EgOVgL/4xfVrCMbhYKUQTdF37SQn4Iw73H5BgCrF1Abdun7Kwy/QZsE/ssAy0y4LxBbvua3PIbFsbRczWWnDdQ==
typescript@^3.9.6:
version "3.9.6"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.6.tgz#8f3e0198a34c3ae17091b35571d3afd31999365a"
integrity sha512-Pspx3oKAPJtjNwE92YS05HQoY7z2SFyOpHo9MqJor3BXAGNaPUs83CuVp9VISFkSjyRfiTpmKuAYGJB7S7hOxw==

uglify-js@^3.1.4:
version "3.8.0"
Expand Down