I'm struggling to fully grasp the definition of Promise
in TypeScript
, as shown below:
/**
* Represents the completion of an asynchronous operation
*/
interface Promise<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
/**
* Attaches a callback for only the rejection of the Promise.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>;
}
My understanding is that
then<TResult1 = T, TResult2 = never>
means then
has two generic types, namely TResult1
and TResult2
. If not specified, TResult1
defaults to T
.
However, it seems that TResult1
is determined by the return type of onfulfilled
. Consider this demo:
interface Result {
status: number;
message: string;
}
function foo() {
return new Promise<Result>(function (resolve, reject) {
resolve({
status: 0,
message: 'ok',
});
});
}
// here fulfilled1's type is: (local function) fulfilled(out: Result): number
foo().then(function fulfilled1(out) {
if (Math.random() > 0.5) {
return 1;
}
});
// here fullfilled2's type is: (local function) fulfilled2(out: Result): string
foo().then(function fulfilled2(out) {
if (Math.random() > 0.5) {
return 'hello';
}
});
Both fulfilled1
and fulfilled2
fit into
then<TResult1 = T, TResult2 = never>
. However, without specifying generic types for then
, I expected TResult1
to be Result
, but it actually becomes number
and string
in these cases.
It seems like I may have misunderstood something about TypeScript
generics. Any insights would be greatly appreciated.