Reputation: 109
I want to define a generic function type named Callbackify in Typescript that does the following things:
I want to use it as a return type for this callbackify function:
function callbackify<E>(executor: () => Promise<E>): Callbackify<E> {
// omitted implementation
}
A few use cases of the function would be:
const fn = callbackify(() => Promise.resolve(4));
// given a callback function as an argument, so should return void
let returnValue = fn((err, value) => { console.log(err, value) })
typeof returnValue // void
//given no argument, so should return Promise<number>
let returnValue = fn()
typeof returnValue // Promise<number>
I have tried the following way but it does not seem to work as expected (it always returns void)
export type CallbackFunction<T> = (error: Error | null, value?: T) => void;
export type Callbackify<T extends CallbackFunction<E>> = (done?: T) => T extends undefined | null ? Promise<E> : void;
Upvotes: 2
Views: 773
Reputation: 250316
I would just use overloads. Overloads are a better match for what you are trying to do:
export type CallbackFunction<T> = (error: Error | null, value?: T) => void;
export type Callbackify<E> = {
(done: CallbackFunction<E>): void;
(): Promise<E>;
}
function callbackify<E>(executor: () => Promise<E>): Callbackify<E> { // Explicit return type not really necesarry
function callbackified(done: CallbackFunction<E>): void;
function callbackified(): Promise<E>;
function callbackified(done?: CallbackFunction<E>): Promise<E> | void {
return null!
}
return callbackified;
}
Upvotes: 2