slorber / awesome-debounce-promise

Debounce your API calls easily and stay in promised land.
https://sebastienlorber.com
391 stars 9 forks source link

Provide TypeScript type definition #1

Closed tkrotoff closed 5 years ago

tkrotoff commented 6 years ago

Would be nice to have a TypeScript type definition

slorber commented 6 years ago

Hi,

I have never really used TS so far nor published any binding, PR welcome if you can help. I'll probably use TS soon so I will probably know better what to do

slorber commented 6 years ago

I started using ts recently, will try to work on this soon

jsamr commented 5 years ago

Here is a draft:

// awesome-debounce-promise.d.ts
declare module 'awesome-debounce-promise' {
    export interface Options {
        /**
         * One distinct debounced function is created per key and added to an internal cache
         * By default, the key is null, which means that all the calls
         * will share the same debounced function
         */
        key?: (...args: any[]) => null|string
        /**
         * By default, a debounced function will only resolve
         * the last promise it returned
         * Former calls will stay unresolved, so that you don't have
         * to handle concurrency issues in your code
         * Setting this to false means all returned promises will resolve to the last result
         * 
         * **Default**: `true`
         */
        onlyResolvesLast?: boolean
    }
    export type PromiseProducer = (...args: any[]) => Promise<any>
    export type AwesomeDebouncePromiseStatic = <P extends PromiseProducer>(fn: P, interval_ms: number, options?: Options) => P
    const AwesomeDebouncePromiseStatic: AwesomeDebouncePromiseStatic
    export default AwesomeDebouncePromiseStatic
}
slorber commented 5 years ago

thanks @jsamr

I really like TS and planning to rewrite all my libs in TS progressively

In the meantime your definition will probably be useful to others ;)

slorber commented 5 years ago

Hi,

I've totally migrated the project to TS so this should be fine now.

However if someone want to give some help, because internally I had to resort to some "as any" to make things compile... Not so easy to type async function wrappers