如何获取Flow类型检查器涵盖的JSON.parse结果? [英] How can I get JSON.parse result covered by the Flow type checker?

查看:72
本文介绍了如何获取Flow类型检查器涵盖的JSON.parse结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Flow.我试图在一个项目上尽可能接近100%的流程覆盖率,而我不知道的一件事是JSON.parse.

I'm new to working with flow. I'm trying to get as close to 100% flow coverage as possible on a project, and one thing I can't figure out is how to handle is JSON.parse.

type ExampleType = {
  thingOne: boolean,
  thingTwo: boolean,
};
const exampleVariable: ExampleType = JSON.parse(
  '{thingOne: true, thingTwo: false}'
);

所以我有一个类型,我从另一个来源收到了一个字符串,我对其进行了解析,并期望它属于该类型.

So I have a type, and I receive a string from another source, and I parse it and expect it to be of that type.

整个JSON.parse(...)部分都标记为流量未覆盖".

The whole JSON.parse(...) section is marked as "Not covered by flow".

如果在文件中使用JSON.parse,是否有办法使文件达到100%的流量覆盖率?如何?流程未覆盖该行时,流程到底在说什么?

Is there a way to get a file to 100% flow coverage if JSON.parse is used in that file? How? What exactly is flow saying when it says that line isn't covered?

推荐答案

问题是JSON.parse返回any.这是签名:

The problem is that JSON.parse returns any. Here is the signature:

static parse(text: string, reviver?: (key: any, value: any) => any): any;

Flow不能保证将解析结果分配给类型ExampleType的正确性,因为谁知道当解析传入的JSON时会发生什么?

Flow cannot guarantee that the assignment of the parse result to the type ExampleType is correct because who knows what will come out when you parse incoming JSON?

但是,如果您使用 flow-validator 进行解析,则可以将覆盖率提高到100%反而.在解析字符串时,据Flow所知,该字符串可能来自任何地方.因此,不能静态保证字符串中的JSON数据具有您期望的形状.什么流程验证器会执行此操作,以提供一个API来描述数据而不是类型的验证架构.解析时在运行时检查架构. Flow-validator会根据您的模式自动生成静态类型,并将成功解析的结果分配给该类型.使用flow-validator的示例如下所示:

But you can get coverage to 100% if you parse with flow-validator instead. When parsing a string as far as Flow knows that string could have come from anywhere. So there is no static guarantee that the JSON data in the string has the shape that you expect. What flow-validator does it to provide an API to describe a validation schema for your data instead of a type. The schema is checked at runtime while parsing. Flow-validator automatically generates a static type from your schema, and assigns the result from a successful parse to that type. Here is what your example looks like with flow-validator:

import { boolean, object } from "flow-validator"

const ExampleSchema = object({
  thingOne: boolean,
  thingTwo: boolean
})

const exampleVariable = ExampleSchema.parse(
  '{"thingOne": true, "thingTwo": false}'
)

您可以检查并确认Flow推断出exampleVariable的正确类型,并且Flow覆盖率现在为100%.如果JSON数据的形状不正确,则ExampleSchema.parse会引发错误.

You can check and see that Flow infers the correct type for exampleVariable, and your Flow coverage is now at 100%. If the JSON data does not have the correct shape then ExampleSchema.parse will throw an error.

您可以从这样的模式中获取类型:

You can get a type from the schema like this:

type ExampleType = typeof ExampleSchema.type

此版本的ExampleType与原始示例中的版本相同.提取类型会自动使您不必为数据结构编写两次形状,而且还可以确保静态类型与运行时验证架构保持同步.

This version of ExampleType is just like the one in your original example. Extracting a type automatically saves you from having to write the shape for your data structure twice, and it also guarantees that the static type stays in sync with the runtime validation schema.

这篇关于如何获取Flow类型检查器涵盖的JSON.parse结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆