Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript complains with and without lib

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.

like image 551
zmbq Avatar asked Dec 05 '17 07:12

zmbq


1 Answers

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.

like image 130
Titian Cernicova-Dragomir Avatar answered Oct 13 '22 10:10

Titian Cernicova-Dragomir