这不会在“尝试流程”上编译:
/* @flow */
type A = { a: number, b: string};
type B = { a: string, b: string };
const x: A = { a:1, b:'2' };
const y: B = { ...x, a: x.a.toString() }
错误是:
const y: B = { ...x, a: x.a.toString() }
^ Cannot assign object literal to `y` because number [1] is incompatible with string [2] in property `a`.
References:
3: type A = { a: number, b: string};
^ [1]
4: type B = { a: string, b: string };
^ [2]
请注意,此代码在 TypeScript 中有效(当我删除字段覆盖时,它无法按预期进行编译)。
如何在不枚举原始对象的所有字段的情况下在 Flow 中实现相同的行为?
最佳答案
计划于下周发布的 Flow v0.111.0 对点差进行了一些重大改进。此代码段现在将进行类型检查。您现在可以在 flow.org/try 中看到它的运行情况。通过切换到“主”版本。
关于javascript - 如何在 Flow 中使用(或模拟)具有覆盖字段的对象传播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52924531/