6

I'm trying to add an async function in a TypeScript project. The code looks like this:

chrome.tabs.onUpdated.addListener(async (id, c, t) => { ... }); 

TypeScript complains:

error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your --lib option

When I add lib: ['es2015'] to tsconfig, TypeScript starts complaining about all calls to console.log saying that console is undefined.

1 Answer 1

5

The default libs for es5 are DOM,ES5, so if you specify es2015 you will also need to add dom explicitly as console is defined in the dom library. Sample tsconfig.json:

{ "compilerOptions": { "target": "es5", "lib": [ "es2015", "dom" ] } } 

Typescript has a modular approach to the default libraries, so you can include only what is available based on your environment.

Sign up to request clarification or add additional context in comments.

5 Comments

This almost works! tslint is still complaining about it! $#@$#@$#@. I'll just tell it to ignore this check.
#@!#@!#!@ I couldn't find a way to disable this message! It's either tslint or PyCharm.
What message are you getting ?
It's PyCharm, not tslint (i've disabled tslint and the problem persists). I get a red squiggly line under the async callback with the exact same message - that there's no Promise constructor or es2015 is not in lib. The code transpiles and runs properly, so this is probably a PyCharm bug.
I ended up putting the callback in its own async function and just pass that function to addListener. The code is now nicer looking and there's no squiggly red line.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.