简体   繁体   English

flowtype 无法返回 object 文字,因为 object 类型不兼容

[英]flowtype cannot return object literal because object type is incompatible

I have the following types:我有以下类型:

export type Transaction = {
  amount: number,
  description: string
}

export type TransactionsForMonth = {
  month: number,
  year: number,
  transactions: Array<Transaction>
}

export type TransactionsGroupedByDay = Array<{
  date: string,
  transactionsForDay: Array<Transaction>
}>

export type TransactionsForMonthGroupedByDay = {
  month: number,
  year: number,
  transactionsForMonth: Array<TransactionsGroupedByDay>,
}

I've created a function that converts from TransactionsForMonth to TransactionsForMonthGroupedByDay , by reducing over the transactions and sorted them into days:我创建了一个 function ,它通过减少交易并将它们分类为天,从TransactionsForMonth转换为TransactionsForMonthGroupedByDay

transactionsGroupedByDay ({ month, year, transactions }: TransactionsForMonth): TransactionsForMonthGroupedByDay {
  // transactions are already sorted in date order
  const transactionsGroupedByDay: TransactionsGroupedByDay = transactions.reduce((transactionsGroupedByDay, transaction) => {
    const thisDay = transactionsGroupedByDay.find(({ date }) => date === transaction.transactionDate)
    const transactionsThisDay = thisDay ? [...thisDay.transactionsForDay, transaction] : [transaction]
    return [
      ...transactionsGroupedByDay.filter(({ date }) => date !== transaction.transactionDate),
      {
        date: transaction.transactionDate,
        transactionsForDay: transactionsThisDay
      }
    ]
  }, [])
  return {
    month,
    year,
    transactionsForMonth: transactionsGroupedByDay
  }
}

When I run flow over this function, I get the following error:当我在这个 function 上运行流程时,我收到以下错误:

Cannot return object literal because object type [1] is incompatible with TransactionsGroupedByDay [2] in array element
of property transactionsForMonth. [incompatible-return]

     src/api/services/TransactionService.js
     55│     return {
     56│       month,
     57│       year,
     58│       transactionsForMonth: transactionsGroupedByDay
     59│     }
     60│   }
     61│

     src/api/types.js
 [1] 35│ export type TransactionsGroupedByDay = Array<{
     36│   date: string,
     37│   transactionsForDay: Array<Transaction>
     38│ }>
       :
 [2] 43│   transactionsForMonth: Array<TransactionsGroupedByDay>,

I am new to flowtype and am wondering what this error means?我是 flowtype 的新手,想知道这个错误是什么意思?

The error is basically saying, the function is supposed to return an object of a given type structure, but your return statement doesn't satisfy that requirement.错误基本上是说, function 应该返回给定类型结构的 object ,但是您的 return 语句不满足该要求。

The reason for that is because in type TransactionsForMonthGroupedByDay you've declared that the property transactionsForMonth is an array of TransactionsGroupedByDay but the variable you actually pass into transactionsForMonth you've declared as const transactionsGroupedByDay: TransactionsGroupedByDay which is incompatible.原因是在type TransactionsForMonthGroupedByDay中,您已声明属性transactionsForMonthTransactionsGroupedByDay的数组,但您实际传递给transactionsForMonth的变量已声明为const transactionsGroupedByDay: TransactionsGroupedByDay ,这是不兼容的。

You can fix this by either fixing your type to accept a single TransactionsGroupedByDay or your variable declaration needs to return an array of TransactionsGroupedByDay .您可以通过修复您的类型以接受单个TransactionsGroupedByDay或您的变量声明需要返回一个TransactionsGroupedByDay数组来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM