简体   繁体   English

如何使用Flow类型执行写运行时类型检查表达式?

[英]How can I do a write run-time type-check expression using Flow type?

Suppose I have two types defined in Flow: 假设我在Flow中定义了两种类型:

type A = {
  x : string; 
};

type B = {
  y : string; 
};

Now I have a function f like this: 现在我有一个函数f像这样:

const f = (o : A | B) : string => {
  if (o isa A) { // Not real code
    return o.x;
  }
  return o.y;
};

How do I implement o isa A ? 如何实施o isa A

I want to create an expression that checks an object against a Flow type-definition at run-time. 我想创建一个表达式,在运行时根据Flow类型定义检查对象。

The compiled code might look like this: 编译后的代码可能如下所示:

const f = o => {
  if (typeof(o.x) !== 'undefined') {
    return o.x;
  }
  return o.y;
};

There are two primary issues here. 这里有两个主要问题。

  1. Nothing says a B -typed object can't have a x property too, just like A , and same for the inverse. 没有什么说过B型对象也不能具有x属性,就像A一样,反之亦然。
  2. Flow isn't quite smart enough to differentiate object types this way. Flow不够智能,无法通过这种方式区分对象类型。

For the first point, to be clear, your existing definition is fine with 首先,要明确一点,您现有的定义适用于

var o: B = {
  x: 45,
  y: "string",
};

because { y: string } means "an object where y is a string ", not "an object with only a y that is a string ." 因为{ y: string }意思是“其中ystring的对象”,而不是“ ystring的对象”。

To get the behavior you are expecting, you'd need to use Flow's Exact Object Syntax as 为了获得您期望的行为,您需要使用Flow的Exact Object Syntax作为

type A = {|
  x : string; 
|};

type B = {|
  y : string; 
|};

Now to the second point, the easiest approach is 现在到第二点,最简单的方法是

const f = (o : A | B) : string => {
  if (typeof o.x !== "undefined") {
    return o.x;
  }
  if (typeof o.y !== "undefined") {
    return o.y;
  }
  throw new Error("Unreachable code path");
};

to make it clear to Flow that the two cases where the properties exist are the only two cases where a string can be returned. 为了使Flow清楚,只有两种可以返回字符串的情况,即存在属性的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在 Typescript 中对这个 object 进行类型检查 - How do I type-check this object in Typescript 使用Flow类型系统执行运行时检查? - Enforcing run-time checks using the Flow type-system? 我是否应该(静态地)对第三方代码进行类型检查(例如使用Flow)? - Should I (statically) type-check Third-party codes (e.g. using Flow)? 如何使用 TypeScript 编译器 API 对使用 `require()` 导入的模块进行类型检查? - How to use the TypeScript Compiler API to type-check modules imported using `require()`? 使用Babel等向Javascript添加运行时类型检查 - Using Babel etc to add run-time type checking to Javascript 如何使用Flow键入默认导出? - How can I type a default export using Flow? 如何在运行时使用JavaScript从Mozilla插件中将XUL作为字符串获取? - How can I get the XUL as a string from a Mozilla add-on at run-time using JavaScript? 如何在流中获取由 Promise 类型包装的类型? - How do I get the type wrapped by a Promise type in flow? 为什么在使用function.apply时Closure不进行类型检查? - Why does not Closure type-check the parameters when using function.apply? 如何获得流程以正确检查类型所实现的接口? - How do I get flow to correctly check the interface being implemented by a type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM