我想使用 typescript 将 html 字符串转换为 deno 中的 html 元素。我为它找到了这个功能:
function createElementFromHTML(htmlString: string) {
var div = document.createElement('div');
div.innerHTML = htmlString.trim();
return div.firstChild;
}
当我用这个命令运行它时:deno run --allow-net .\scripts\API\fetchUtils.ts
它告诉我:
Cannot find name 'document'. Do you need to change your target library? Try changing the
lib
compiler option to include 'dom'.ts(2584)
经过一番搜索,我发现我需要将它添加到我的 tsconfig 中:
{
"compilerOptions": {
"allowJs": true,
"esModuleInterop": true,
"lib": ["esnext", "DOM"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"pretty": true,
"resolveJsonModule": true,
"target": "esnext"
},
"include": [
"./**/*.ts"
]
}
我仍然遇到同样的错误,重新启动等没有帮助。我不知道这是否有所不同,但从我开始从控制台运行 deno 文件的角度来看,这就是我的项目的结构方式:
scripts
-API
-fetchUtils.ts
tsconfig.json
最佳答案
见 vscode_deno #318deno.config
可能未设置为“./tsconfig.json”(或其路径)
如果您使用的是 vscode,只需按照提到的问题进行操作。
如果没有,do configuration
关于typescript - 找不到名称 'document' 。你需要改变你的目标库吗?尝试更改 `lib` 编译器选项以包含 'dom' .ts(2584),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65156598/