You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The docs for isValid currently state the following:
Returns false if argument is Invalid Date and true otherwise. Argument is converted to Date using toDate. See toDate Invalid Date is a Date, whose time value is NaN.
Convert the given argument to an instance of Date.
If the argument is an instance of Date, the function returns its clone.
If the argument is a number, it is treated as a timestamp.
If the argument is none of the above, the function returns Invalid Date.
Note: all Date arguments passed to any date-fns function is processed by toDate.
Stating that the input can be either Date or number. However, the type is as follows:
Implying that string should work as well. Upon testing, toDate('2024-02-13T09:42:53.057Z') correctly parses as "Tue Feb 13 2024 10:42:53 GMT+0100" leading me to believe that:
The docs for toDate()do not correctly reflect that it also supports ISO strings as input, while the types do.
Since isValid() refers to toDate() for its behavior, ISO strings should also be valid input for isValid().
The latter, however, appears to be an incorrect assumption as isValid('2024-02-13T09:42:53.057Z') returns false rather than true, which is also reflected by the implementation:
Based on the above I'm inclined to think the isValid() documentation shouldn't point to toDate() but instead just state that the input is expected to be either a date object or a number in order to validate. I also think a big source of confusion is the fact that the the isValid argument is typed as unknown. I get why since you can throw any value at it, but at the same time it hides critical information and might even be considered an anti-pattern in TS unless you're working with numbers, since any date object will be valid by default. And especially since v3 now allows string input pretty much everywhere, the current behavior of isValid is a bit unexpected:
All functions that accept date arguments now also accept strings.
Happy to help out if we find common ground 🙂.
The text was updated successfully, but these errors were encountered:
The docs for isValid currently state the following:
with a type of
Going to the toDate docs we see the following:
Stating that the input can be either
Date
ornumber
. However, the type is as follows:Implying that
string
should work as well. Upon testing,toDate('2024-02-13T09:42:53.057Z')
correctly parses as "Tue Feb 13 2024 10:42:53 GMT+0100" leading me to believe that:toDate()
do not correctly reflect that it also supports ISO strings as input, while the types do.isValid()
refers totoDate()
for its behavior, ISO strings should also be valid input forisValid()
.The latter, however, appears to be an incorrect assumption as
isValid('2024-02-13T09:42:53.057Z')
returnsfalse
rather thantrue
, which is also reflected by the implementation:date-fns/src/isValid/index.ts
Lines 37 to 43 in 6f44a16
Based on the above I'm inclined to think the
isValid()
documentation shouldn't point totoDate()
but instead just state that the input is expected to be either a date object or a number in order to validate. I also think a big source of confusion is the fact that the theisValid
argument is typed asunknown
. I get why since you can throw any value at it, but at the same time it hides critical information and might even be considered an anti-pattern in TS unless you're working with numbers, since any date object will be valid by default. And especially since v3 now allowsstring
input pretty much everywhere, the current behavior ofisValid
is a bit unexpected:Happy to help out if we find common ground 🙂.
The text was updated successfully, but these errors were encountered: