no-misused-promises
禁止在未设计用于处理 Promises 的地方使用 Promises.
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
此规则禁止向逻辑位置提供 Promise,例如 TypeScript 编译器允许但未正确处理的位置的 if 语句。这些情况通常是由于缺少 await
关键字或只是对异步函数处理/等待方式的误解而发生的。
¥This rule forbids providing Promises to logical locations such as if statements in places where the TypeScript compiler allows them but they are not handled properly.
These situations can often arise due to a missing await
keyword or just a misunderstanding of the way async
functions are handled/awaited.
no-misused-promises
仅检测向不正确的逻辑位置提供 Promises 的代码。有关检测未处理的 Promise 语句,请参阅 no-floating-promises
。
¥no-misused-promises
only detects code that provides Promises to incorrect logical locations.
See no-floating-promises
for detecting unhandled Promise statements.
- 扁平配置
- 旧版配置
export default tseslint.config({
rules: {
"@typescript-eslint/no-misused-promises": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-misused-promises": "error"
}
};
在线运行试试这个规则 ↗
选项
该规则接受以下选项:
type Options = [
{
/** 是否在向条件语句提供 Promise 时发出警告。 */
checksConditionals?: boolean;
/** 当 `...` 扩展 `Promise` 时是否发出警告。 */
checksSpreads?: boolean;
/** 是否在从类型为返回 `void` 的函数返回 Promise 时发出警告。 */
checksVoidReturn?: /**
* 是否在从类型为返回 `void` 的函数返回 Promise 时发出警告。
* 是否禁用检查所有异步函数。
*/
| boolean
/** 哪些形式的函数可能已禁用检查。 */
| {
/** 禁用检查作为参数传递的异步函数,其中参数类型预期为返回 `void` 的函数。 */
arguments?: boolean;
/** 禁用检查作为 JSX 属性传递的异步函数,该函数预期为返回 `void` 的函数。 */
attributes?: boolean;
/** 禁用检查在扩展或实现另一种类型的类型中的异步方法,该方法预期该方法返回 `void`。 */
inheritedMethods?: boolean;
/** 禁用检查作为对象属性传递的异步函数,该函数预期为返回 `void` 的函数。 */
properties?: boolean;
/** 禁用检查在返回类型为返回 `void` 的函数的函数中返回的异步函数。 */
returns?: boolean;
/** 禁用检查用作变量的异步函数,该变量的返回类型为返回 `void` 的函数。 */
variables?: boolean;
};
},
];
const defaultOptions: Options = [
{ checksConditionals: true, checksSpreads: true, checksVoidReturn: true },
];
¥Options