flowtype 可空对象不可变 属性 细化

flowtype nullable object immutable property refinement

我想对可以为 null 的 属性 使用优化。这个带有选中 属性 的对象然后作为参数传递给函数。

/* @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
}

Try flow

它不应该与具有可变 属性 的对象一起使用,因为此 属性 稍后可以更改为 null。所以这就是我使用不可变 属性 的原因。但是还是不行。

有没有办法用精炼的属性传递对象?

您可以尝试使用间接转换,将类型转换为 any,因为 any 接受任何类型的值作为输入。然后因为 any 类型还允许您从中读取所有可能的类型,所以您可以将 any 值分配给您的类型,因为 any 也是您的类型。

/* @flow */
const a: {+foo: ?string} = {};

const fun = (obj: {+foo: string}) => {
  return obj
}

if (a.foo) {
  fun((a: any))
}

精炼对象的 属性 精炼 属性,而不是对象。

// `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)

归根结底,没有超级直接的方法可以将 { +foo: ?string } 转换为 { +foo: string },因为它们是两种完全不同的复杂类型,必须这样处理。