Flow: Nested unions issue/question

Created on 1 Feb 2017  路  2Comments  路  Source: facebook/flow

Why flow can't infer that OneActionType can be used where ActionType is used in following example:

/* @flow */

type ActionTypes = 'ONE' | 'TWO' | 'THREE'

type ActionType = {
  type: ActionTypes
}

type OneActionType = {
  type: 'ONE';
  payload: string
}

// this is ok:
// (x:OneActionType) => (x.type:ActionTypes)
// this is not:
(x:OneActionType) => (x:ActionType)

Try Flow

Most helpful comment

ActionType.type is read/write. OneActionType.type is only 'ONE' but if is treated as a ActionType it could have 'TWO' or 'THREE' assigned to it, violating its type.

You need to mark ActionType.type as covariant (read only): Try Flow

All 2 comments

ActionType.type is read/write. OneActionType.type is only 'ONE' but if is treated as a ActionType it could have 'TWO' or 'THREE' assigned to it, violating its type.

You need to mark ActionType.type as covariant (read only): Try Flow

@jesseschalken thank you!

Leave here good reading about variance in flow.

Was this page helpful?
0 / 5 - 0 ratings