当数据与声明的类型不匹配时,flow不会失败

时间:2018-06-15 15:11:10

标签: types flowtype

我在src/types中声明了我的流类型,并将目录添加到libs下的.flowconfig。但是,下面提供的示例不起作用!

declare type MyCustomAction = {|
  type: "MY_CUSTOM_ACTION",
  payload: string
|}

declare type MyCustomAction2 = {|
  type: "MY_CUSTOM_ACTION",
  payload: string
|}

declare type CustomAction = MyCustomAction2 | MyCustomAction

然后像这样的动作创建者:

const MyCustomActionCreator = (): Function => (dispatch: Dispatch<MyCustomAction>, getState: FlockGetState): void => {
  dispatch({
    type: MY_CUSTOM_ACTION,
    payload: {
      name: 12
    }
  })
}

和这样的减速器:

export const INITIAL_STATE: reducerState = Object.freeze({})

export default (state: reducerState = INITIAL_STATE, action: CustomAction): reducerState => {

  switch (action.type) {
  case MY_CUSTOM_ACTION:
    return {
      ...state,
      name: Object.freeze({ ...INITIAL_STATE, loading: action.payload.data.name })
    }
  }
}

1)在动作创建者中调度的动作不符合我的类型 2)reducer正在访问两个都不存在的字段

我希望我声明的类型存在,因为我已将它们添加到目录中: src/types

我期望的行为发生在try flow:https://flow.org/try/#0FDAmFMGMBsEMCdwAIAuBPADsgsmgwgK4DOKA9gLYCCkKAlqQHZIC8SA3gD7BI+-pYAuJACJsATQD6eAKoBlACoB5bBMp55ASUUA5YQBpuvHhlhpopWKCEl4tBgHNDPDgF8nSdxBgJk-HPmIyKhp6BgAmFnYuIx4-IVFJGQVlVXUtXQMYpBMzCyskGztHI1d3Tyg4RFRMZEISCmo6RkjcOqDG0IiOJFbAhpDGEB4vSt8apAARWiITFEgAC0oABQ0AHkoAPkiAClgBhiFKAEoWLcoAbkMRn2qsSenZhfWhNlvwIQASWQIAIz9VwoOLYuLasKYzWBzRYrdYbS7uSCMEg9AL1YJNBh4RCQ0jwHZHIQAMQIDH2pyQ21AD0hCyE4Me81WvTRHUYGz0SHs4BQshQkPABKQADdSLRQOS2O5htSodtJVlYjUhABycRSORKFRqTQ6ZWZBU5cyWF5SmIMWDkd5IACMYVNPDcWRcR3cbmAfiQiFABEg4HgvP5kVgDDQQ144AAHhhcSgkIiGMiNNoNJpKAAZCQKSjyACiQi9Pr9AZQyFYih+ACsoCgAHQAM0Q4AAXuA5c6ykZI9H4LGIHXYARoLHtiR+fnwN7ff6+SXIkmUxp05n5Nmcxy9hihG1+hjBQWp8XS1t5bxTUQAO60KEUjehGt+E4noyQWBEZCqxIalLa9LKgT2z1uQIeAmCfBUawg0cS31BUkHNS0hHLKsaHrRsWzlJAIJredUwzLNcw5I0qQcIRb0YGtDTyGtQEhWAa3g5B21gx0YhYh0gA

此外,如果我从类型声明中完全删除其中一个声明的类型,则流仍会传递,而不会抱怨该类型不存在!我唯一的结论是,这些类型在某种程度上被全局声明为any。任何想法都会受到欢迎!

0 个答案:

没有答案