具有精确流类型的对象传播 [英] Object Spread with Exact Flow types

查看:117
本文介绍了具有精确流类型的对象传播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在流程中获得与对象传播一致的确切类型。

I can't seem to get exact types in flow to work with object spread.

type Point = {| x: number, y: number |};
const p1: Point = { x: 10, y: 10 };
const p2: Point = { ...p1, y: 5 };

生成错误对象文字。不精确类型与确切类型不兼容

这不会产生错误,但会修改p1:

This does't produce an error, but modifies p1:

const p3: Point = Object.assign(p1, {y: 5});

将Object.assign与空对象一起使用也会生成相同的对象文字错误:

Using Object.assign with an empty object also produces the same object literal error:

const p4: Point = Object.assign({}, p1, {y: 5});

如果我使用键入Point = {x:number,y:number} ; 然后对象传播有效,但理想情况下我想使用确切的类型。

If I use type Point = {x: number, y: number}; then object spread works, but ideally I'd like to use an exact type.

推荐答案

是的,这是一个已知的错误。我目前正致力于改进对象传播的分析,以解决这个问题和其他问题。根本原因是对象扩散表达式导致未密封,这与精确对象类型不兼容。改进的分析将尽可能创建密封对象。

Yeah, this is a known bug. I'm currently working to improve our analysis for object spread to fix this and other issues. The underlying cause is that object spread expressions result in "unsealed" which are incompatible with exact object types. The improved analysis will create sealed objects when possible.

这篇关于具有精确流类型的对象传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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