LoginSignup
8
4

More than 5 years have passed since last update.

Flowtypeのactionの型付け。String Literal型とString型について

Posted at

flowtypeでactionの型付けをする際に、定数として宣言したアクション名をtypeofを使いActionの型に変形するパターンを使うことが多いと思います。(スクショはAtom-IDE)

スクリーンショット 2017-09-18 06.06.37.png

この場合、{type: string}型として認識されています。

ここで、const action: 'Action' = 'Action'のようにString Literal型にすると型が明確になります。

スクリーンショット 2017-09-18 06.08.07.png

これにすると、デバックが容易になるだけでなく、開発時においてアクションをディスパッチする際の型が明確なので迷うことがなくなります。(画像はAtom-IDE)

stringバージョン

スクリーンショット 2017-09-18 06.26.57.png

string literalバージョン

スクリーンショット 2017-09-18 06.15.55.png

気持ち

コード量は増えるのでそのトレードオフを考える必要はあります。
またそれを改善する記事を今週中には上げる予定です。

なにか議論あればコメント欄またはtwitterまでよろしくお願いします。

関連記事

flowtypeによるredux-thunkの型付け
http://qiita.com/akameco/items/c7e48e4666dc1b17350c

Flowtype+reduxにおけるreducerの正しい型付け
http://qiita.com/akameco/items/fe7ba22c158a2593b077

Atom-IDEについて
http://qiita.com/akameco/items/d883094c5c8152915cf2

8
4
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
8
4