添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
魁梧的灭火器  ·  GitHub - ...·  4 周前    · 
玩篮球的松树  ·  ApolloX - ...·  6 月前    · 
果断的猴子  ·  ubuntu 14.04 安装 ...·  7 月前    · 
千杯不醉的核桃  ·  Solved: How can I get ...·  12 月前    · 
有胆有识的香槟  ·  Jupyter ...·  1 年前    · 
2
0

More than 1 year has passed since last update.

typescript-eslintのawait-thenableルールについて

Posted at

この記事では typescript-eslint のルールのうち await-thenable について解説します。
typescript-eslintの他のルールについても別記事で解説し、最終的にReact開発を行うにあたって最適なルール設定を構築することを目的としています。

ルールについて

await-thenable await の利用をthenableなオブジェクト以外に出来ないようにするルールで、 "plugin:@typescript-eslint/recommended-requiring-type-checking" で設定されるルールの一つです。
thenableなオブジェクトとはPromiseのように then メソッドを持つオブジェクトのことを指します。

thenableなオブジェクトのイメージ
type Thenable<T> = {
  then: (onfulfilled: (arg: T) => unknown) => unknown;

そしてこのルールは以下のようなケースで違反になります。

const hoge = () => 'hello';
await hoge();

解決するためには、hoge自体をthenableなオブジェクトにするか、awaitを外します。

thenableなオブジェクトにする
const hoge = async () => 'hello';
await hoge();
awaitを外す
const hoge = () => 'hello';
hoge();
ルールの採用について

不必要なawaitを配置することで想定外な動作を起こすことはありませんが、必要な場合でなければawaitを使えないようにすることでミスを減らせたり可読性を向上させられるのでこのルールは採用した方が良いと考えています。

このルールが有効であることでこのようなミスを防げる
const hoge = async () => 'hello';
// hogeの呼び出しを忘れている
await hoge;
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0