- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想对可以为 null 的属性进行优化。然后将这个具有 checked 属性的对象作为参数传递给函数。
/* @flow */
const a: {+foo: ?string} = {};
const fun = (obj: {+foo: string}) => {
return obj
}
if (a.foo) {
fun(a) // null or undefined [1] is incompatible with string
}
它不应该与具有可变属性的对象一起使用,因为稍后可以将此属性更改为 null
。所以这就是我使用不可变属性的原因。但它仍然不起作用。
有没有办法传递具有精炼属性的对象?
最佳答案
细化对象的属性是细化属性,而不是对象。
// `a.foo` is of type `?string`
// `a` is of type `{+foo: ?string}`
if (a.foo) {
// within this block, `a.foo` is of type `string` (no `?`)
// `a` is of type `{+foo: ?string}`
}
// `a.foo` is of type `?string`
// `a` is of type `{+foo: ?string}`
在这种特殊情况下,我可能会这样做:
if (a.foo) {
fun({ foo: a.foo });
}
( Try )
只是因为它是如此简单的案例。在更复杂的情况下,您需要使用 disjoint unions .
type A = {
+foo: string,
};
type B = {
+foo: void,
};
type T = A | B;
const a: T = ({ foo: undefined }: B);
const fun = (obj: A) => {
return obj
}
// `a` is of type `A | B` (which is type `T`)
if (a.foo) {
// inside this block `a` is of type `A`
fun(a);
}
// `a` is of type `A | B` (which is type `T`)
( Try )
归根结底,没有 super 直接的方法可以将 { +foo: ?string }
转换为 { +foo: string }
因为它们是两个完全不同的复杂类型,必须这样处理。
关于javascript - flowtype 可空对象不可变属性细化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58011622/
我正在尝试缩小此图像,但它会不断失真。 这是我应用细化的相关代码。我也尝试过使用“thin”功能而不是“skeletonize”,但结果相似。 from skimage.morphology impo
好的,据我所知,目前我们有两种方法 首先是我可以在给定脚本中使用的一些 javascript。但这会关闭所有内容的右键单击。 window.oncontextmenu = function() {
在 MarkLogic 中如何找到元素所在的文档 URI 匹配的值为1742 这里是示例 XML 文档: 1742 1742 TT 1742
摘要:我有一个数据集,其收集方式使得维度最初不可用。我想获取本质上是一大块无差别的数据,并为其添加维度,以便可以对其进行查询、子集化等。这是以下问题的核心。 这是我拥有的 xarray 数据集: D
我有一个大型数据集(请参阅下面的示例格式),我需要进行以下思考: 确定第 1、2、5 列中出现的重复值 - 如果全部重复,那么我需要删除多余的行并对第 8 列中的值进行平均(这对于我将发布的代码是成功
我正在做一个练习项目,其中 iOS 应用程序打印来自 jsonplaceholder.typicode.com 的/posts 列表,当用户选择一个时,加载详细 View Controller 并显示
module Access def last self[-1] end def start_end self[0] + last end end module Stri
我是一名优秀的程序员,十分优秀!