1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Flux Standard Action に従った Flowtype でのジェネリックな Action の定義

Last updated at Posted at 2018-11-21

こんな感じ。

// @flow
export type Action<T: string, P> =
  | {
      type: T,
      payload: P,
      meta?: mixed
    }
  | {
      type: T,
      payload: Error,
      error: true,
      meta?: mixed
    };

こうしておけば errortrue かどうかで payload の型が決まる。

// @flow
type HogeAction = Action<"HOGE", string>

if (action.type === "HOGE")
  if (action.error) {
    // action.payload is Error
  } else {
    // action.payload is string
  }
}

他の方法

ググると次のような奴が見つかった。
でもこれだと errortrue かどうかで payload の型が決まることはない。

// @flow
type Action<A: string, P> = {|
  type: A,
  payload?: P | Error,
  error?: boolean,
  meta?: mixed
|};

参考

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?