Typescript promise generic type

GenericsTypescript

Generics Problem Overview


I've a sample Promise function like below. On success I return a number and on false I return string. The compiler is complaining to specify some kind of generic type to the promise. In this case what type I've to specify? Do I've to specify like Promise<number> or Promise<number | string>?

function test(arg: string): Promise {
    return new Promise((resolve, reject) => {
        if (arg === "a") {
            resolve(1);
        } else {
            reject("1");
        }
    });
}

Generics Solutions


Solution 1 - Generics

The generic type of the Promise should correspond to the non-error return-type of the function. The error is implicitly of type any and is not specified in the Promise generic type.

So for example:

function test(arg: string): Promise<number> {
    return new Promise<number>((resolve, reject) => {
        if (arg === "a") {
            resolve(1);
        } else {
            reject("1");
        }
    });
}

Solution 2 - Generics

function test(arg: string) {
   return new Promise((resolve: (value: number) => void, reject) => {
    if (arg === "a") {
        resolve(1);
    } else {
        reject("1");
    }
  });
}

Solution 3 - Generics

another solution

P.S. (by the way)

> Generic type 'Promise' requires 1 type argument(s).ts(2314)

function test(arg: string){
    return new Promise<number>((resolve, reject) => {
        if (arg === "a") {
            resolve(1);
            // (parameter) resolve: (value: number | PromiseLike<number>) => void
        } else {
            // (parameter) reject: (reason?: any) => void
            reject("1");
        }
    });
}

enter image description here

Solution 4 - Generics

You could define a custom Promise type that actually also cares about the type of the rejection. You can also just give it a single type and the reject type will be any, like in a normal promise.


type CustomPromise<T, F = any> = {
	catch<TResult = never>(
		onrejected?: ((reason: F) => TResult | PromiseLike<TResult>) | undefined | null
	): Promise<T | TResult>;
} & Promise<T>;

function test(arg: string): CustomPromise<number, string> {
	return new Promise((resolve, reject) => {
		if (arg === "a") {
			resolve(1);
		} else {
			reject("1");
		}
	});
}

const myPromise = test("a");
myPromise.then((value) => {}); //value is of type `number`
myPromise.catch((reason) => {}); //reason is of type `string`

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVJAIView Question on Stackoverflow
Solution 1 - GenericsDave TemplinView Answer on Stackoverflow
Solution 2 - GenericslynnicView Answer on Stackoverflow
Solution 3 - GenericsxgqfrmsView Answer on Stackoverflow
Solution 4 - GenericsNibujaView Answer on Stackoverflow