gpt4 book ai didi

flowtype - "exponentially large number of cases"最新流中的错误,具有常见的传播模式

转载 作者:行者123 更新时间:2023-12-03 17:12:57 24 4
gpt4 key购买 nike

我经常使用以下模式来创建对象 null/undefined省略属性:

const whatever = {
something: true,
...(a ? { a } : null),
...(b ? { b } : null),
};

从流程版本 v0.112 开始,这会导致错误消息:

Computing object literal [1] may lead to an exponentially large number of cases to reason about because conditional [2] and conditional [3] are both unions. Please use at most one union type per spread to simplify reasoning about the spread result. You may be able to get rid of a union by specifying a more general type that captures all of the branches of the union.



在我看来,这并不是真正的类型错误,只是 Flow 试图避免一些繁重的计算。这导致我的项目中出现了数十个流程错误,我需要以某种方式解决这些错误。是否有一些优雅的方法可以为这些提供更好的类型信息?我不想修改代码的逻辑,我相信它会按照我需要的方式工作(除非有人在这里也有更优雅的解决方案)。在我求助之前在这里问 // $FlowFixMe对于所有这些。

Complete example on Try Flow

最佳答案

写起来并不优雅,我认为 Flow 应该处理你所展示的情况,但如果你仍然希望 Flow 对它进行类型检查,你可以尝试像这样重写它:

/* @flow */

type A = {
cat: number,
};

type B = {
dog: string,
}

type Built = {
something: boolean,
a?: A,
b?: B,
};

function buildObj(a?: ?A, b?: ?B): Built {
const ret: Built = {
something: true
};

if(a) ret.a = a
if(b) ret.b = b

return ret;
}

Try Flow

关于flowtype - "exponentially large number of cases"最新流中的错误,具有常见的传播模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58844133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com