Redux reducer的默认语句中的奇怪类型空流语法

Redux reducer的默认语句中的奇怪类型空流语法,redux,flowtype,Redux,Flowtype,我在这里搜索使用flow with redux的方法时找到了代码示例: 特殊语法为(action:empty);它仅仅是一种流魔法,打算在switch语句的默认情况下使用,还是有其他用途 它看起来像是没有返回值类型但参数类型为奇怪的“empty”的不合适的函数类型语句,我找不到相关文档 // @flow type State = { +value: boolean }; type FooAction = { type: "FOO", foo: boolean }; type BarActio

我在这里搜索使用flow with redux的方法时找到了代码示例:

特殊语法为(action:empty);它仅仅是一种流魔法,打算在switch语句的默认情况下使用,还是有其他用途

它看起来像是没有返回值类型但参数类型为奇怪的“empty”的不合适的函数类型语句,我找不到相关文档

// @flow
type State = { +value: boolean };

type FooAction = { type: "FOO", foo: boolean };
type BarAction = { type: "BAR", bar: boolean };

type Action = FooAction | BarAction;

function reducer(state: State, action: Action): State {
  switch (action.type) {
    case "FOO": return { ...state, value: action.foo };
    case "BAR": return { ...state, value: action.bar };
    default:
      (action: empty);
      return state;
  }
}

empty
是流的。我相信它最初引入的主要动机是对称,但事实证明它有一些用途。正如您所确定的,在这种情况下,它可以用来使流强制执行穷尽性。它可以类似地用于一系列
if
/
else
语句中

然而,当您想要流时,它可以随时使用,以防止任何实际值在某处结束。这是非常模糊的,所以这里有几个例子:

// Error: empty is incompatble with implicitly-returned undefined
function foo(): empty {
}

// No error since the function return is not reached
function foo2(): empty {
  throw new Error('');
}

function bar(x: empty): void {
}

// Error: too few arguments
bar();
// Error: undefined is incompatible with empty
bar(undefined);

foo
示例中,我们可以看到流强制执行在返回
empty
的函数中永远不会到达
返回
。在
bar
示例中,我们可以看到流阻止函数被调用。

很好地解释了empty,谢谢,有没有url可以让我阅读更多关于它的信息?另外,如果能了解更多关于(action:empty);-它既不是返回类型,也不像函数参数类型。巴贝尔不知怎么地把它传送出去了吗?如果它被记录在什么地方,我不知道。此语法只是一个类型断言。您可以在左侧放置任何表达式,在右侧放置任何类型。好的,这是为没有键的对象定义类型的唯一方法<代码>键入emptyObj={[string]:empty}