Skip to main content

no-misused-promises

Disallow Promises in places not designed to handle them.

💭

该规则需要 类型信息 才能运行。


此规则禁止向逻辑位置提供 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 仅检测向错误逻辑位置提供 Promise 的代码。 请参阅 no-floating-promises 来检测未处理的 Promise 语句。

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-misused-promises": "error"
}
};
在线运行试试这个规则 ↗

选项

checksConditionals

如果不想检查条件,可以用 "checksConditionals": false 配置规则:

英:If you don't want to check conditionals, you can configure the rule with "checksConditionals": false:

{
"@typescript-eslint/no-misused-promises": [
"error",
{
"checksConditionals": false
}
]
}

这样做可以防止规则查看像 if (somePromise) 这样的代码。

英:Doing so prevents the rule from looking at code like if (somePromise).

此规则与 checksConditionals: true 的代码示例:

英:Examples of code for this rule with checksConditionals: true:

const promise = Promise.resolve('value');

if (promise) {
// Do something
}

const val = promise ? 123 : 456;

while (promise) {
// Do something
}
Open in Playground

checksVoidReturn

同样,如果你不想在预期返回 void 的情况下检查返回 Promise 的函数,你的配置将如下所示:

英:Likewise, if you don't want to check functions that return promises where a void return is expected, your configuration will look like this:

{
"@typescript-eslint/no-misused-promises": [
"error",
{
"checksVoidReturn": false
}
]
}

你可以通过提供禁用特定检查的对象来禁用 checksVoidReturn 选项的选择性部分。 支持以下选项:

英:You can disable selective parts of the checksVoidReturn option by providing an object that disables specific checks. The following options are supported:

  • arguments: 禁用检查作为参数传递的异步函数,其中参数类型需要返回 void 的函数
  • attributes: 禁用检查作为 JSX 属性传递的异步函数,该函数应返回 void
  • properties: 禁用检查作为对象属性传递的异步函数,该函数预期是返回 void 的函数
  • returns: 禁用检查返回类型为返回 void 的函数的函数中返回的异步函数
  • variables: 禁用检查用作返回类型为返回 void 的函数的变量的异步函数

例如,如果你不介意将 () => Promise<void> 传递给 () => void 参数或 JSX 属性可能会导致浮动的未处理 Promise:

英:For example, if you don't mind that passing a () => Promise<void> to a () => void parameter or JSX attribute can lead to a floating unhandled Promise:

{
"@typescript-eslint/no-misused-promises": [
"error",
{
"checksVoidReturn": {
"arguments": false,
"attributes": false
}
}
]
}

此规则与 checksVoidReturn: true 的代码示例:

英:Examples of code for this rule with checksVoidReturn: true:

[1, 2, 3].forEach(async value => {
await doSomething(value);
});

new Promise(async (resolve, reject) => {
await doSomething();
resolve();
});

const eventEmitter = new EventEmitter();
eventEmitter.on('some-event', async () => {
synchronousCall();
await doSomething();
otherSynchronousCall();
});
Open in Playground

checksSpreads

如果你不想检查对象分布,可以添加以下配置:

英:If you don't want to check object spreads, you can add this configuration:

{
"@typescript-eslint/no-misused-promises": [
"error",
{
"checksSpreads": false
}
]
}

此规则与 checksSpreads: true 的代码示例:

英:Examples of code for this rule with checksSpreads: true:

const getData = () => someAsyncOperation({ myArg: 'foo' });

return { foo: 42, ...getData() };

const getData2 = async () => {
await someAsyncOperation({ myArg: 'foo' });
};

return { foo: 42, ...getData2() };
Open in Playground

何时不使用它

在设置了许多滥用 Promise 的大型现有项目中,很难启用此规则。 或者,如果你不担心浮动或误用 Promise 导致的崩溃(例如,如果你注册了全局未处理的 Promise 处理程序),那么在某些情况下,不使用此规则可能是安全的。 你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

英:This rule can be difficult to enable on large existing projects that set up many misused Promises. Alternately, if you're not worried about crashes from floating or misused Promises -such as if you have global unhandled Promise handlers registered- then in some cases it may be safe to not use this rule. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

进一步阅读

选项

该规则接受以下选项

type Options = [
{
checksConditionals?: boolean;
checksSpreads?: boolean;
checksVoidReturn?:
| {
arguments?: boolean;
attributes?: boolean;
properties?: boolean;
returns?: boolean;
variables?: boolean;
}
| boolean;
},
];

const defaultOptions: Options = [
{ checksConditionals: true, checksVoidReturn: true, checksSpreads: true },
];

资源