Skip to content

Commit

Permalink
simplify types
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Sep 13, 2018
1 parent 4345d7a commit 21a7058
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/create-thunks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface Action {
interface ReduxAction {
type: string;
}

Expand All @@ -10,9 +10,13 @@ interface GetState {
(): unknown;
}

type OnlyPromise<T> = T extends (...args: any[]) => Promise<infer R>
? Promise<R>
: T extends (...args: any[]) => infer R ? R : void;
/**
* For functions return its return type otherwise return void.
*
* Different from the core ReturnType which can oly be called for
* functions.
*/
type FunctionReturnValue<T> = T extends (...args: any[]) => infer R ? R : void;

class SimpleStore {
private _reduxDispatch: ReduxDispatch;
Expand All @@ -24,7 +28,9 @@ class SimpleStore {
this.dispatch = this.dispatch.bind(this);
}

dispatch<T extends Function | Action>(action: T): OnlyPromise<T> {
dispatch<T extends Function | ReduxAction>(
action: T,
): FunctionReturnValue<T> {
const ret = this._reduxDispatch(action);

if (typeof action === "function") {
Expand Down

0 comments on commit 21a7058

Please sign in to comment.