目录

@typescript-eslint/no-misused-promises

禁止在不正确的位置使用Promise。

规则配置

// code-linter.json5
{
  "rules": {
    "@typescript-eslint/no-misused-promises": "error"
  }
}

选项

详情请参考@typescript-eslint/no-misused-promises选项。

正例

export async function func(): Promise<void>{
  const promise = Promise.resolve('value');

  // Always `await` the Promise in a conditional
  if (await promise) {
    // Do something
  }

  const val = await promise ? '123' : '456';
  console.log(`${val}`);

  while (await promise) {
    // Do something
  }
}

反例

export async function func(): Promise<void>{
  const promise = Promise.resolve('value');
  // 默认条件语句中需要使用await Promise
  if (promise) {
    // Do something
  }

  // 默认条件语句中需要使用await Promise
  const val = promise ? '123' : '456';
  console.log(`${val}`);

  // 默认条件语句中需要使用await Promise
  while (promise) {
    // Do something
  }
}

规则集

plugin:@typescript-eslint/all

Code Linter代码检查规则的配置指导请参考Code Linter代码检查(ArkTS/TS)。