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
{{ message }}
Repository navigation
TS2532 Compound null check, object cannot be undefined #35260
For the record, the expression form x || y doesn't prove anything about either x or y except that "at least one of them is truthy--but we don't know which." This is not enough information to narrow either one.
I have a similar condition that has been plaguing my ability to refactor long conditions into local variables.
This actually breaks when we apply a suggested refactoring.
exportdefaultfunctionactOnDataOrUndefined({ data }: {data?: string[]}){// This is fineconstok=data&&data.length!=0 ? actOnData(data) : null;// Refactor the condition to a local variable and typescript // reports that the call to actOnData is invalid because data can be undefined// when it clearly(?) cannot be, since hasData is true only when data is not undefined.consthasData=data&&data.length!=0;constnot_ok=hasData ? actOnData(data) : null;}functionactOnData(data: string[]){returndata.length;}
Hi, I may be a noob, but I was wondering if it would be possible to detect that createdAt || version.createdAt is the same expression in both cases. Of course, it would only work if the compounds are primitive types like string or number.
Activity
AnyhowStep commentedon Nov 21, 2019
TS, in general, doesn't do multi-variable narrowing where the narrowing of variable
xalso narrows variabley.It can get very expensive and complicated to do so.
Off topic, seeing logical operators used on non-boolean operands triggers me =x
You should write something like,
RyanCavanaugh commentedon Nov 22, 2019
Narrowing can't produce conditional forms like what would be needed to handle this
fatcerberus commentedon Nov 25, 2019
For the record, the expression form
x || ydoesn't prove anything about eitherxoryexcept that "at least one of them is truthy--but we don't know which." This is not enough information to narrow either one.Duplicate (pretty much) of #31603
fpintos commentedon Mar 21, 2021
I have a similar condition that has been plaguing my ability to refactor long conditions into local variables.
This actually breaks when we apply a suggested refactoring.
For example, see this code in playground:
sdandois commentedon Jan 27, 2023
Hi, I may be a noob, but I was wondering if it would be possible to detect that
createdAt || version.createdAtis the same expression in both cases. Of course, it would only work if the compounds are primitive types like string or number.For example, you could pre-process the code into: