Dor*_*rad 7 dom node.js typescript
在扩展 Cheerio 库时,我实现了以下静态函数(其他扩展函数工作正常):
$.nodeType = function (elem: CheerioElement): number {
switch (elem.type) {
case "comment":
return Node.COMMENT_NODE; // <--- it fails here
case "tag":
return Node.ELEMENT_NODE; // <--- it fails here
case "text":
return Node.TEXT_NODE; // <--- it fails here
default:
return -1;
}
};
Run Code Online (Sandbox Code Playgroud)
运行时出现如下错误(编译tsc -b成功):
ReferenceError:节点未定义
Node接口是DOMAPI的一部分。因此,我意识到需要DOM在.compilerOptionstsconfig.json
但是,我仍然遇到运行时错误。
的最小相关部分tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"incremental": true,
"lib": [
"esnext",
"dom"
],
"module": "commonjs",
"noImplicitAny": true,
"outDir": "./lib/",
"sourceMap": true,
"target": "esnext",
"watch": true
},
"include": [
"./src/**/*.ts",
]
}
Run Code Online (Sandbox Code Playgroud)
我想在包含该函数的Node特定.ts文件中显式导入 lib,但我没有找到任何“可包含”的标准DOM库。
| 归档时间: |
|
| 查看次数: |
2553 次 |
| 最近记录: |