[Typescript] Understanding “lib” and ES libraries

Sometime if our tsconfig.json looks like this;

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "outDir": "dist"
    }
}

 

We can get all kinds of type autocomplete in VSCode such as :

Array.isArray([])

 

But we don't have:

Array.from(document.queryElmentsById('button'))

it shows red underline.

 

This is because we are just targeting "es5":

"target": "es5",

 

To solve the issues, we can use:

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es6"],
        "module": "commonjs",
        "outDir": "dist"
    }
}

 

But now, we have another problem for "docuemnt".

to fix this:

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es6", "dom"],
        "module": "commonjs",
        "outDir": "dist"
    }
}

 

posted @ 2020-10-05 17:46  Zhentiw  阅读(186)  评论(0编辑  收藏  举报