跳转到内容

noMisusedPromises

此内容尚不支持你的语言。

Disallow Promises to be used in places where they are almost certainly a mistake.

In most cases, if you assign a Promise somewhere a Promise is not allowed, the TypeScript compiler will be able to catch such a mistake. But there are a few places where TypeScript allows them — they’re not necessarily a mistake — even though they could be considered almost certainly to be one.

This rule disallows using Promises in such places.

const promise = Promise.resolve('value');
if (promise) { /* This branch will always execute */ }
const promise = Promise.resolve('value');
const val = promise ? 123 : 456; // Always evaluates to `123`.
// The following filter has no effect:
const promise = Promise.resolve('value');
[1, 2, 3].filter(() => promise);
const promise = Promise.resolve('value');
while (promise) { /* This is an endless loop */ }
// Using a `Promise` as an iterable expands to nothing:
const getData = () => fetch('/');
console.log({ foo: 42, ...getData() });
// These `fetch`-es are not `await`-ed in order:
[1, 2, 3].forEach(async value => {
await fetch(`/${value}`);
});
const promise = Promise.resolve('value');
if (await promise) { /* Do something */ }
const val = (await promise) ? 123 : 456;
while (await promise) { /* Do something */ }
const getData = () => fetch('/');
console.log({ foo: 42, ...(await getData()) });
// for-of puts `await` in outer context:
for (const value of [1, 2, 3]) {
await doSomething(value);
}
biome.json
{
"linter": {
"rules": {
"nursery": {
"noMisusedPromises": "error"
}
}
}
}