この記事では
typescript-eslint
のルールのうち
await-thenable
について解説します。
typescript-eslintの他のルールについても別記事で解説し、最終的にReact開発を行うにあたって最適なルール設定を構築することを目的としています。
await-thenable
は
await
の利用をthenableなオブジェクト以外に出来ないようにするルールで、
"plugin:@typescript-eslint/recommended-requiring-type-checking"
で設定されるルールの一つです。
thenableなオブジェクトとはPromiseのように
then
メソッドを持つオブジェクトのことを指します。
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を外す