Flow和Ramda动态使用?

时间:2017-06-16 01:50:00

标签: flowtype ramda.js

我不知道在哪里报告这个问题,或者它是否可以修复,但我认为Flow至少应该找到一个这样的简单案例。

const someComponent = (props: Props) =>
   const { children, doesntExist } = props; // Flow error here, great.
   const anotherOne = pick(['doesntExist'], props); // no error here
   // ...

它不应该支持这种情况吗?这是一个错误吗?应该是ramda's还是flow的贡献者手?

1 个答案:

答案 0 :(得分:0)

Original Flow type signatures look like this

declare function pick<A>(
  keys: Array<string>,
): (val: { [key: string]: A }) => { [key: string]: A };
declare function pick<A>(
  keys: Array<string>,
  val: { [key: string]: A }
): { [key: string]: A };

我们可以将其更改为

declare function pick <T>(
  keys: Array<$Keys<T>>,
  val: T
): $Shape<T>;
declare function pick <T>(
  keys: Array<$Keys<T>>
): (T) => $Shape<T>;

结果将是

type Props = {
  children: string,
  val: number,
}
const someComponent = (props: Props) => {
  const p1 = pick(['children1'], props); // Flow error here
  const p2 = pick(['children'], props);
  (p2: {children: string});
  // welp, but this is sad
  (p2: {val: number});
}

您可以在Flow playground

中使用它

如果可以选择TypeScript,请签出monocle-tsIt provided Flow signatures some times ago, but dropped it,您可以尝试将其复活并放入流式仓库中。

UPD:也许idx是您要寻找的

const p1 = idx(props, (_) => _.children1); // Flow error here

您可以在Flow playground

中使用它