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
constdate=getDate();functiongetDate(){returnDate.now()>1000 ? newDate() : null;}consty=!date||date.valueOf()>1000;if(y){console.log("y is truthy");}else{console.log(getMonth(date));}functiongetMonth(value: Date){returnvalue.getMonth();}
Expected behavior:
No error trying to pass date to getMonth, since it has been proved to be a non-null date due to variable y being false.
Actual behavior:
The following error is displayed for date:
Argument of type 'Date | null' is not assignable to parameter of type 'Date'. Type 'null' is not assignable to type 'Date'.
Note that if you do if (!date || date.valueOf() > 1000 instead of if (y), the error disappears, so that one small bit of indirection seems to throw off the compiler.
TypeScript Version: 4.03
Search Terms: Date, truthy, inference
Code
Expected behavior:
No error trying to pass
date
togetMonth
, since it has been proved to be a non-null date due to variabley
being false.Actual behavior:
The following error is displayed for
date
:Note that if you do
if (!date || date.valueOf() > 1000
instead ofif (y)
, the error disappears, so that one small bit of indirection seems to throw off the compiler.Playground Link: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAJgQygUxgXhgc2VAIk5ACgEoBuAKHIDMBXMYKAS3Cx3xRJgG9yY+YATjhoCwMdsgB0YEAHdOAPhgBGAAzqYAfhhhks8QU4AuHTQA2ZigF9KoSLACe6GAEJEKGAB9P8ApIBuCGY0yADyVIoq6qoU5IxUMIQOxNy8-HYQIGZSZiCYhABETowQMFACNFAAFg4FZORWMMhmEKg8-DAZWTl5hNhQALLg1YTuyMT1NtR0DMxi-UNgI4HByCYSKe38QlAiYishkgvDVSTWQA
Related Issues: (not able to find any -- not sure what other key words to search for)
The text was updated successfully, but these errors were encountered: