'Typescript undefined date validation
I am using luxon for my app. I have some undefined date. I validate my date with luxon's isDateTime and function returns string. I used this function multiple places of the app. Most of the places I am passing date string and only places I am getting undefined date. I manage to silent the Typescript error but I think it's not optimal solution.
code-sandbox
Is there any better way, I can make this function?
const {
DateTime
} = require('luxon')
const ISO_DATE_FORMAT = "yyyy-MM-dd";
const dateToStrings = (date: string | number | Date): DateTime =>
DateTime.fromISO(new Date(date).toISOString());
const dateToISOString = (date: Date | string | undefined): string => {
if (DateTime.isDateTime(date)) {
return dateToStrings(date).toFormat(ISO_DATE_FORMAT);
}
return date as string;
};
console.log(dateToISOString(undefined));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
