Krisna
Krisna

Reputation: 3423

Typescript Unsafe return of an `any` typed value

I am creating few date functions for my app and for date validation I am using luxon. All of the functions, I first convert them into string then I am formatting the date. Instead of writing same line every time(to convert them into string), I have decided to create a separate function for this. When add this function my format function, I am getting typescripts error: 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)

Upvotes: 0

Views: 5708

Answers (1)

Matthieu Riegler
Matthieu Riegler

Reputation: 54963

Fixing the types in your function should fix you issues :

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);

Upvotes: 2

Related Questions