`any` 类型值的 Typescript 不安全 return
Typescript Unsafe return of an `any` typed value
我正在为我的应用程序创建一些日期函数,我正在使用 luxon 进行日期验证。所有的功能,我首先将它们转换成字符串,然后我格式化日期。我决定为此创建一个单独的函数,而不是每次都写相同的行(将它们转换成字符串)。将此功能添加到我的格式功能时,出现打字稿错误:Unsafe return of an
any typed value
Error image
import { DateTime } from 'luxon'
const dateToStrings = (date: Date): string => DateTime.fromISO(new Date(date).toISOString())
export const formatDateMonthYear = (date: string | number | Date): string =>
dateToStrings(date).toFormat(DEFAULT_DATE_FORMAT)
export const formatTime = (date: string | number | Date): string =>
dateToStrings(date).toFormat(SHORT_TIME_FORMAT)
export const formatYearMonthDate = (date: string | number | Date): string =>
DateTime.fromISO(new Date(date).toISOString()).toFormat(ISO_DATE_FORMAT)
修复函数中的类型应该可以解决您的问题:
const dateToStrings = (date: string | number | Date): DateTime =>
DateTime.fromISO(new Date(date).toISOString());
export const formatDateMonthYear = (date: string | number | Date): string =>
dateToStrings(date).toFormat(DEFAULT_DATE_FORMAT);
export const formatTime = (date: string | number | Date): string =>
dateToStrings(date).toFormat(SHORT_TIME_FORMAT);
我正在为我的应用程序创建一些日期函数,我正在使用 luxon 进行日期验证。所有的功能,我首先将它们转换成字符串,然后我格式化日期。我决定为此创建一个单独的函数,而不是每次都写相同的行(将它们转换成字符串)。将此功能添加到我的格式功能时,出现打字稿错误:Unsafe return of an
any typed value
Error image
import { DateTime } from 'luxon'
const dateToStrings = (date: Date): string => DateTime.fromISO(new Date(date).toISOString())
export const formatDateMonthYear = (date: string | number | Date): string =>
dateToStrings(date).toFormat(DEFAULT_DATE_FORMAT)
export const formatTime = (date: string | number | Date): string =>
dateToStrings(date).toFormat(SHORT_TIME_FORMAT)
export const formatYearMonthDate = (date: string | number | Date): string =>
DateTime.fromISO(new Date(date).toISOString()).toFormat(ISO_DATE_FORMAT)
修复函数中的类型应该可以解决您的问题:
const dateToStrings = (date: string | number | Date): DateTime =>
DateTime.fromISO(new Date(date).toISOString());
export const formatDateMonthYear = (date: string | number | Date): string =>
dateToStrings(date).toFormat(DEFAULT_DATE_FORMAT);
export const formatTime = (date: string | number | Date): string =>
dateToStrings(date).toFormat(SHORT_TIME_FORMAT);