Skip to content
← Back to rules

unicorn/no-thenable Correctness

This rule is turned on by default.

它的作用

禁止定义 then 属性。

为什么这不好?

如果一个对象被定义为“thenable”,一旦它被意外地 用于 await 表达式中,可能会导致问题。

示例

以下是此规则的错误代码示例:

javascript
async function example() {
  const foo = {
    unicorn: 1,
    then() {},
  };

  const { unicorn } = await foo;

  console.log("after"); // <- 这永远不会执行
}

以下是此规则的正确代码示例:

javascript
async function example() {
  const foo = {
    unicorn: 1,
    bar() {},
  };

  const { unicorn } = await foo;

  console.log("after");
}

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-thenable": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-thenable": "error",
  },
});
bash
oxlint --deny unicorn/no-thenable

版本

此规则在 v0.0.13 中加入。

参考资料