小编Ice*_*nge的帖子

找不到名称“文档”。您需要更改目标库吗?尝试更改“lib”编译器选项以包含“dom”.ts(2584)

我想用 typescript 将 html 字符串转换为 deno 中的 html 元素。我找到了这个功能:

function createElementFromHTML(htmlString: string) {
  var div = document.createElement('div');
  div.innerHTML = htmlString.trim();

  return div.firstChild; 
}
Run Code Online (Sandbox Code Playgroud)

当我使用以下命令运行它时:

deno run --allow-net .\scripts\API\fetchUtils.ts

它告诉我这一点:

找不到名称“文档”。您需要更改目标库吗?尝试更改lib编译器选项以包含 '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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我仍然遇到同样的错误,重新启动等没有帮助。

我不知道这是否有什么不同,但从我从控制台开始运行 deno 文件的角度来看,这就是我的项目的结构:

scripts
  -API
    -fetchUtils.ts
tsconfig.json
Run Code Online (Sandbox Code Playgroud)

typescript tsconfig deno

9
推荐指数
1
解决办法
9019
查看次数

Return 2 counted values from one function

I want to create a function which returns two counted values. The values are counted by iterating over a for-loop.

For example, I have an array of persons (male, female, adults, children) and I only want to find the amount of boys (child + male) and the amount of women (adult + female).

In the last few years I've been writing in javascript and this is how I would have done it in javascript.

function countBoysAndWomen() {
    var womenCounter = …
Run Code Online (Sandbox Code Playgroud)

java

7
推荐指数
1
解决办法
139
查看次数

Typescript:为什么 PromiseLike 在异步函数的对象中可能是 this 类型?

我写了这个函数,但它抛出一个错误,我不明白为什么......

const createObject = async (): Promise<MyType> => {
  await Promise.resolve();
  return {
    async foo() {
      await this.bar();
      // do something else
    },
    async bar() {
      await Promise.resolve();
      // do something here
    }
  };
};

interface MyType {
  foo: () => Promise<void>;
  bar: () => Promise<void>;
}
Run Code Online (Sandbox Code Playgroud)

为什么第 5 行的函数调用this.bar()显示错误?它告诉我它this是类型MyTypePromiseLike<MyType>. 第二种可能的类型是问题,但我不知道在哪种情况下这可能是PromiseLike?难道不能假设如果调用该函数,则它始终是“已加载”吗object

解决这个问题最优雅的方法是什么?目前我正在这样做(这绝对是丑陋的,并且作为代码的读者无法理解):

await (await this).bar();
Run Code Online (Sandbox Code Playgroud)

我还发现设置函数的返回类型createObjectfromPromise<MyType>可以Promise<any>解决问题,但这不是很准确。

typescript

5
推荐指数
1
解决办法
2191
查看次数

检查两个数字是正数还是两个都是负数

我没有找到这个问题的更好的名字......

我想检查两个数字是否都小于0,0或两者都大于0.是否有比这更简单的方法?

if (nr0 < 0 && nr1 < 0 || nr0 == 0 && nr1 == 0 || nr0 > 0 && nr1 > 0) {
    //do smth...
}
Run Code Online (Sandbox Code Playgroud)

javascript

2
推荐指数
1
解决办法
945
查看次数

标签 统计

typescript ×2

deno ×1

java ×1

javascript ×1

tsconfig ×1