Say I have the following code:
function fn(): Promise<string> {
return new Promise((resolve, reject) => {
let randomBit = (Math.random() * 10) % 2 === 0;
if (randomBit) {
resolve("Resolve");
} else {
reject(0);
}
});
}
The above method, which returns a valid Promise, is correctly typed as Promise<string>
. I understand that the internal type declares the type returned in the case of a successful resolution. But, what about the return of a rejection? In my example the rejection would resolve to number
. Shouldn't, the return signature of fn()
be something like
Promise<string, number>
?
Or, more generally, shouldn't a Promise declare the generic types for the two possible outcomes, like
Promise<TSuccess, TError>
?
I feel like I'm missing something.