In my function, I am checking whether an unknown value resembles a Date
object:
function looksLikeDate(obj: unknown): obj is { year: unknown; month: unknown; day: unknown } {
return (
obj !== null && typeof obj === "object" && "year" in obj && "month" in obj && "day" in obj
);
}
However, I encounter an error related to the "year" in obj
part of the code:
The object could possibly be 'null'. (2531)
Interestingly, when I interchange obj !== null
and
typeof obj === "object"
, the error disappears: TS Playground Link
Doesn't this seem peculiar? Can someone provide an explanation for this behavior?