erikras / redux-promise-listener

A Redux middleware that allows actions to be converted into Promises
MIT License
44 stars 11 forks source link

Add typescript types declaration #3

Open govorov opened 6 years ago

govorov commented 6 years ago

Hi,

redux-promise-listener doesn't have typescript type definitions at the moment. I tried to convert existing flow type definitions and I ended up with this:

declare module 'redux-promise-listener' {

    import { Store } from 'redux';

    export type State = any;

    export interface Action {
        type: string;
        payload?: any;
    }

    export type Next = (action: Action) => State;

    export type SetPayload = (action: Action, payload: any) => Action;

    export type GetPayload = (action: Action) => any;

    // I'm not sure about this generic
    export type Middleware<S = any> = (store: Store<S>) => (
        (next: Next) => (
            (action: Action) => State
        )
    );

    export interface Config {
        start: string;
        resolve: string;
        reject: string;
        setPayload?: SetPayload;
        getPayload?: GetPayload;
        getError?: GetPayload;
    }

    export interface AsyncFunction {
        asyncFunction: (...args: Array<any>) => Promise<any>;
        unsubscribe: () => void;
    }

    export type PromiseListener = {
        middleware: Middleware,
        createAsyncFunction: (config: Config) => AsyncFunction
    };

    export default function createListener(): PromiseListener;

}

I'm not proposing a pull request because I'm not sure if this declaration is correct and whether you find this idea useful.

Thank you.

thchia commented 6 years ago

I think you can depend on the redux type definition for Middleware, rather than re-typing it here.