【问题标题】:Argument of type 'string | Date' is not assignable to parameter of type 'Date''string | 类型的参数日期”不可分配给“日期”类型的参数
【发布时间】:2021-04-02 18:25:05
【问题描述】:

我试图通过模拟日期来编写测试套件。我得到这个错误'Argument of type 'string |日期'不可分配给'日期'类型的参数。'。这是函数

const getDay = (inputDate: any, today = new Date()) => {
  const reportDate = new Date(inputDate)
  if (today >= reportDate) {
    const differenceInTime = today.getTime() - reportDate.getTime()
    const differenceInDays = differenceInTime / (1000 * 3600 * 24)
    const finalDayDiff = Math.floor(differenceInDays)
    if (finalDayDiff === 0) {
      return translate("common.today")
    } else if (finalDayDiff === 1) {
      return finalDayDiff + translate("common.dayAgo")
    } else {
      return finalDayDiff + translate("common.daysAgo")
    }
  } else {
    return ""
  }
}

失败的测试套件 testName 和 todayDate 以红色突出显示

import { getDay } from "../common-functions"
import { translate } from "../../i18n/translate"

describe("checkForDifferenceInDays", () => {
  Array.of(
    [
      "1. difference in days - 1 day ago",
      new Date("2021-03-18T00:00:00.000Z"),
      1 + translate("common.dayAgo"),
    ],
    [
      "2. difference in days - 5 day ago",
      new Date("2021-03-22T00:00:00.000Z"),
      5 + translate("common.daysAgo"),
    ],
    [
      "3. difference in days - today",
      new Date("2021-03-17T00:00:00.000Z"),
      translate("common.today"),
    ],
    ["4. empty string", new Date("2021-03-15T00:00:00.000Z"), ""],
  ).forEach((testCase) => {
    const [testName, todayDate, expectedResult] = testCase
    const reportDate = "2021-03-17T00:00:00.000Z"
    test(**testName**, () => {
      //Act
      const actualResult = getDay(reportDate, **todayDate**)
      //Assert
      expect(actualResult).toEqual(expectedResult)
    })
  })
})
Highlighted in bold shows an error. can someone help what i am missing

【问题讨论】:

    标签: javascript typescript jestjs


    【解决方案1】:

    这是Array<string | Date>类型的混合数组:

    [
      "1. difference in days - 1 day ago",
      new Date("2021-03-18T00:00:00.000Z"),
      1 + translate("common.dayAgo"),
    ],
    

    这里不需要Array.of。为了使数组元素具有精确的类型,它应该是一个常量:

      ([
        [
          "1. difference in days - 1 day ago",
          new Date("2021-03-18T00:00:00.000Z"),
          1 + translate("common.dayAgo"),
        ],
        ...
      ] as const).forEach(...)
    

    【讨论】:

      【解决方案2】:

      您的测试用例当前适用于数组数组,每个内部数组都包含stringDate 类型的元素。 (这可以在 TypeScript 中显式表示为类型 (string | Date)[][]Array<Array<string | Date>>。)

      您可以尝试在此处使用 元组 数组而不是数组数组。 (这可以在 TypeScript 中明确表示为类型 [string, Date, string][]Array<[string, Date, string]>。)元组元素的类型更加明确,因此在解构 forEach 回调函数中的元素时,这可能会正常工作。

      为了更清楚,我稍微重构了您的代码。希望你不要介意。

      import { getDay } from "../common-functions"
      import { translate } from "../../i18n/translate"
      
      describe("checkForDifferenceInDays", () => {
        const arr: [string, Date, string][] =
          [
            [
              "1. difference in days - 1 day ago",
              new Date("2021-03-18T00:00:00.000Z"),
              1 + translate("common.dayAgo")
            ],
            [
              "2. difference in days - 5 day ago",
              new Date("2021-03-22T00:00:00.000Z"),
              5 + translate("common.daysAgo"),
            ],
            [
              "3. difference in days - today",
              new Date("2021-03-17T00:00:00.000Z"),
              translate("common.today"),
            ],
            [
              "4. empty string",
              new Date("2021-03-15T00:00:00.000Z"),
              ""
            ]
          ]
      
        arr.forEach((testCase) => {
          const [testName, todayDate, expectedResult] = testCase
          const reportDate = "2021-03-17T00:00:00.000Z"
          test(testName, () => {
            //Act
            const actualResult = getDay(reportDate, todayDate)
            //Assert
            expect(actualResult).toEqual(expectedResult)
          })
        })
      })
      

      【讨论】:

        猜你喜欢
        • 2023-01-11
        • 2022-01-01
        • 2017-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多