typescript - 找不到名称 'document' 。你需要改变你的目标库吗?尝试更改 `lib` 编译器选项以包含 'dom' .ts(2584)

标签 typescript tsconfig deno

我想使用 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/

相关文章:

Angular 2 从 RxJs 订阅返回数据

angular - Angular 应用程序中的空注释

deno - 如何在 Deno REPL 中导入模块?

typescript - 如何覆盖 Angular-CLI 项目中的 node_module 错误类型?

node.js - 同时安装 Node 和 Deno 的最高效的 docker 镜像

node.js - Deno:如何处理异常

javascript - Atom 中的atom-typescript 不会捕获错误或在 typescript 文件中显示语法颜色

node.js - 组合 Visual Studio Code 构建任务

typescript - 在 TypeScript 中使单个属性可选

typescript - tsconfig 和环境声明中的项目引用