我已经在尝试流程编辑器中复制了我的情况,可以访问here。
这里是代码,以防链接出现问题:
/* @flow */
type PayloadType = 1 | 2 | 3;
type Transaction = {
amount: number,
destination: string
}
function create(type: PayloadType, transaction: Transaction): void {
transaction.amount = 10;
transaction.destination = "8ca76aff-8fe8-4715-9e9a-2ad0630d45a0"
if ((type: PayloadType) === 4) {
transaction.message = "Hello";
}
}
const transaction: Transaction = {}
create(1, transaction)
错误是:
无法将
"Hello!"
分配给transaction.message
,因为message
Transaction
中缺少属性1。
我分配无效属性的行永远不会执行。我假设Flow应该知道这一点,因为type
永远不能为4
,因此条件永远不会为真。
EDIT:这是更实际的示例,为link。
[我认为这里的问题不是该错误出现,而是该错误出现为时已晚,如果要删除type
语句中的if
的转换,则错误会较早出现,我相信是更期望的结果。
/* @flow */
type PayloadType = 1 | 2 | 3;
type Transaction = {
amount: number,
destination: string
}
function create(type: PayloadType , transaction: Transaction): void {
transaction.amount = 10;
transaction.destination = "8ca76aff-8fe8-4715-9e9a-2ad0630d45a0"
if (type === 4) {
transaction.message = "Hello";
}
}
const transaction: Transaction = {}
create(1, transaction)