typescript/await-thenable Correctness
作用
此规则不允许对非 Thenable 的值使用 await。
为什么这很糟糕?
虽然在 JavaScript 中对非 Promise-like 的值使用 await 是合法的(它会立即解析),但对于不了解这一行为的读者来说,这种做法可能会令人困惑。它也可能是程序员错误的迹象,例如忘记添加括号来调用一个返回 Promise 的函数。
示例
以下是此规则的错误代码示例:
await 12;
await (() => {});
// 非 Promise 值
await Math.random;
await { then() {} };
// 这不是一个 Promise - 它是一个返回 Promise 的函数
declare const getPromise: () => Promise<string>;
await getPromise;以下是此规则的正确代码示例:
await Promise.resolve('value');
await Promise.reject(new Error());
// 类 Promise 值
await {
then(onfulfilled, onrejected) {
onfulfilled('value');
},
};
// 这是一个 Promise - 由调用函数产生
declare const getPromise: () => Promise<string>;
await getPromise();如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"options": {
"typeAware": true
},
"rules": {
"typescript/await-thenable": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
options: { typeAware: true },
rules: {
"typescript/await-thenable": "error",
},
});bash
oxlint --type-aware --deny typescript/await-thenable版本
此规则于 v1.12.0 中加入。
