类型与流类型不兼容

时间:2017-12-15 12:15:24

标签: javascript types flowtype

我有一个代码(不考虑moment,我添加了流程的存根)

/* @flow */
const moment = (a?: any) => a
const badDateRegex = /(\d|\d\d)\/(\d|\d\d)\/\d{4}/

const makeDatePretty = (date: string) => {
  if (badDateRegex.test(date)) {
    const [m, d, y] = date.split('/')
    return `${y}-${m}-${d}`
  }
  return date
}
const format = 'MM/YYYY'

const convertDateToMY = (date: Object | string) => moment.isMoment(date)
  ? typeof date.format === 'function' && date.format(format)
  : typeof date === 'string'
    ? moment(makeDatePretty(date))
    : date != null
      ? moment(date)
      : moment()

const convertDatesToMY = (dates: string[] | string | Object[], separator: string = ' - ') =>
  Array.isArray(dates)
    ? dates.map(convertDateToMY).join(separator)
    : convertDatesToMY(dates)

流量抛出错误

22: const convertDatesToMY = (dates: string[] | string | Object[], separator: string = ' - ') =>
                                     ^ string. This type is incompatible with
22: const convertDatesToMY = (dates: string[] | string | Object[], separator: string = ' - ') =>
                                                         ^ object type
22: const convertDatesToMY = (dates: string[] | string | Object[], separator: string = ' - ') =>
                                                         ^ object type. This type is incompatible with
22: const convertDatesToMY = (dates: string[] | string | Object[], separator: string = ' - ') =>
                                     ^ string

我在类型声明中遗漏了什么?

Link of Flow Playground

0 个答案:

没有答案