- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们正在尝试在 Flow 中进行类型优化,以防止值在外部接口(interface)进入我们的应用程序。为此,我们使用 mixed
,然后尝试细化为已知类型,但 Flow 并不容易!
下面看起来应该可行,我已经验证了 mixed
类型值符合 response
类型的要求。
type Response = { head: string }
const refineToResponse = (value: mixed): ?Response => {
if (value
&& typeof value === "object"
&& typeof value.head === "string") {
return value;
}
return null;
};
但我只是收到一条非常无用的错误消息:
16: return value;
^^^^^ object. This type is incompatible with the expected return type of
11: const refineToResponse = (value: mixed): ?Response => {
^^^^^^^^ object typeProperty
head
is incompatible:11: const refineToResponse = (value: mixed): ?Response => {
^^^^^ mixed. This type is incompatible with
8: head: string
^^^^^^ string
编辑:
最佳答案
那是不安全的。如果某物在运行时具有 string
类型,这并不意味着它具有相同的静态类型,例如它可能是一些枚举:'Foo' | 'Bar'
,因此将其设为 string
将允许不安全的突变。另一方面,它可以是 number | string
,所以将来 head
真的可以变成数字或任何类型。
相反,您可以执行以下操作:
const refineToResponse = (value: mixed): ?Response => {
if (value
&& typeof value === "object"
&& typeof value.head === "string") {
return { head: value.head };
}
return null;
};
关于javascript - 在 Flow 中精炼混合到已知对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45276951/
我正在构建一个简单的产品过滤器,并将规范分为不同类型。 我希望我的逻辑是这样的: 如果选择 2 个规范,每个规范都来自不同类型,则产品应与两个选择相匹配。 如果选择 3 种规范,其中 2 种来自相同类
我正在尝试使用 OpenCV 和 Haar 级联制作手部检测程序。它工作得很好,但它非常生涩。所以我在问自己,这是否是 haar 文件的问题,它太“便宜”了,或者是否有一种方法可以通过使用轮廓或特征检
所有很棒的答案!但这个问题值得细化...... 我有以下示例 XML ... .... ...以及以下 XPath 查
我正在尝试将多项式拟合到我的数据,例如 import scipy as sp x = [1,6,9,17,23,28] y = [6.1, 7.52324, 5.71, 5.86105, 6.3, 5
我有一个(swift)类,它有一个指向委托(delegate)的弱指针,如下所示: import UIKit @objc public protocol DRSlidingPanelViewContr
我是一名优秀的程序员,十分优秀!