流程:无法将_分配给_,因为_中缺少属性_

问题描述 投票:0回答:1

我已经在尝试流程编辑器中复制了我的情况,可以访问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

javascript flowtype
1个回答
1
投票

[我认为这里的问题不是该错误出现,而是该错误出现为时已晚,如果要删除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)

Flow Try for change

© www.soinside.com 2019 - 2024. All rights reserved.