Typescript promise generic type

I have a Promise function as shown below. On successful return, I return number and false, I return string . The compiler complains to indicate some general type of promise. In this case, what type should I specify? Should I indicate as Promise<number> or Promise<number | string> Promise<number | string> ?

 function test(arg: string): Promise { return new Promise((resolve, reject) => { if (arg === "a") { resolve(1); } else { reject("1"); } }); } 
+8
generics typescript
source share
1 answer

The generic Promise type must match the return function type without error. The error is implicitly of type any and is not specified in the generic type of Promise.

So for example:

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