TypeScript promise constructor declaration
I was answering this question when I noticed strange behavior in the TypeScript declaration of the Promise constructor:
import * as Bluebird from 'bluebird'
interface PromiseConstructor {
new <R>(callback: (resolve: (thenableOrResult?: R | PromiseLike<R>) => void, reject: (error?: any) => void, onCancel?: (callback: () => void) => void) => void): Bluebird<R>;
}
declare var Promise: PromiseConstructor;
// This works:
const x: PromiseLike<any> = new Promise<any>((a,b) => void 0);
// This does not work:
import('jquery').then($ => {
console.log($.fn.jquery)
});
// This doesn't work either:
async function test() {
return "";
}
As you can see, I declare a constructor and it allows me to build a promise and it doesn't blame it for not being a PromiseLike type. But I cannot use async functions or lazy imports. The compiler Version 2.5.0-dev.20170629
blames this message twice:
test.ts (13,1): error TS2712: "Promise" constructor is required to dynamically call imports in ES5 / ES3. Make sure you have a declaration for the "Promise" constructor, or include "ES2015" in your -lib option.
How do I declare a PromiseConstructor like this?
+1
source to share
No one has answered this question yet
See similar questions:
or similar: