Description
The current construct signature for Promise in es6.d.ts is this:
new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
I would like all of these to definitely compile:
new Promise(resolve => resolve()); // (1)
new Promise<void>(resolve => resolve(null)); // (2)
new Promise<number>(resolve => resolve(5)); // (3)
... these to maybe compile:
new Promise<void>(resolve => resolve()); // (4)
new Promise(resolve => resolve(5)); // (5), and also inferred as Promise<number>
... and this to not compile:
new Promise<number>(resolve => resolve()); // (6)
Currently all of these compile, including (6), although (5) is inferred as Promise<{}>
Changing value
to required makes (6) not compile, but also breaks (1) and (4). By then adding one more overload:
new (executor: (resolve: () => void, reject: (reason?: any) => void) => void): Promise<void>;
new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
(1) to (3) will continue compiling, and (4) to (6) will stop compiling. Since (5) was not being inferred correctly anyway, I think it's okay that it won't compile. Losing (4) is perhaps the bigger problem, since it will break existing code that uses it.
PromiseConstructor.resolve already does this - it has one overload with no type parameter and no parameter that returns Promise<void>
, and one with a type parameter and a required parameter that returns Promise<T>
. I seem to remember Promise's construct signature also did used to do the same at one point (maybe back when it was in lib.d.ts?) which is why I was surprised to discover that it doesn't now.