[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"
    }
}
原文地址:https://www.cnblogs.com/Answer1215/p/13770573.html