javascript - 为什么空对象对流类型中的非精确类型注解有效?

标签 javascript flowtype

我的意思是这样的代码

const someObj: { id: number } = {};
const num: number = someObj.id;
console.log(num);

num 这里是未定义的,绝对不是数字

可以在此处查看代码片段 https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoAxnAdgZwC5g5wC2ApgPIBGAVgFxgDeYAlgCb1YCuxlpATmAC+YALyNBAbgzZ8YLsQ7deAsUTJVqAOjYSgA

最佳答案

这是一个 documented limitation对于未密封的对象:

Unsealed objects allow new properties to be written at any time. Flow ensures that reads are compatible with writes, but does not ensure that writes happen before reads (in the order of execution).

This means that reads from unsealed objects with no matching writes are never checked. This is an unsafe behavior of Flow which may be improved in the future.

所以通常流可以检查id是否写成数字,但不能检查id是否真的被写入了someObj

关于javascript - 为什么空对象对流类型中的非精确类型注解有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62728413/

相关文章:

javascript - 如何使用javascript更改具有同一类的多个div的宽度?

javascript - Vue 属性未定义

javascript - JSON jQuery 数据变量

javascript - 完整日历,将逻辑应用于 defaultDate 设置

php - 如何在 POST 中以 AJAX 方式发送信息?

javascript - 使用 Flow.js 在 React 中自动完成自定义按钮组件的 vs-code

javascript - 真实检查后访问可选 Prop 的流程错误

javascript - if 流中的分支

Javascript Flow 泛型函数语法

flowtype - 导入 CSS 文件时出现流 “Required module not found”