gpt4 book ai didi

javascript - 流量: Cannot assign _ to _ because property _ is missing in _

转载 作者:行者123 更新时间:2023-12-01 00:21:53 27 4
gpt4 key购买 nike

我在 try Flow 编辑器中重现了我的情况,可以访问 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)

错误是:

Cannot assign "Hello!" to transaction.message because property message is missing in Transaction 1.

我分配无效属性的行永远不应该被执行。我假设 Flow 应该知道这一点,因为 type 永远不可能是 4,因此条件永远不会为 true。

编辑:这是一个link举一个更现实的例子。

最佳答案

我认为这里的问题不是出现错误,而是错误出现得太晚了,如果在 if 语句中删除 type 的转换,错误会更早出现,什么我相信这是更预期的结果。

/* @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

关于javascript - 流量: Cannot assign _ to _ because property _ is missing in _,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59341958/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com