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
False negatives and false positives for "Object is possibly 'null'" warning in typescript@next with "complex" if #10198
classNested{data: string;}classFoo{bar: Nested|null=null;baz: number=1;getdata(): null|string{if(this.bar===null||this.baz!==1){returnnull;}else{returnthis.bar.data;// ^~~~~ Object is possibly 'null'. (BUT IT CAN'T BE NULL.)}}}
Expected behavior:
No error.
Actual behavior:
An error.
Details:
This failure must be new. I think I installed typescript@next last week and had no error. I installed it today and it throws an error now.
If a you remove the second if-branch (this.baz !== 1) it works.
This also throws:
get data(): null | string {
if (this.bar === null) {
return null;
} else if (this.baz !== 1) {
return null;
} else {
return this.bar.data;
// ^~~~~ Object is possibly 'null'. (BUT IT CAN'T BE NULL.)
}
}
This works:
get data(): null | string {
if (this.baz !== 1) {
return null;
} else if (this.bar === null) {
return null;
} else {
return this.bar.data;
}
}
(!) On the other hand: This works, but could be wrong so it should throw an error!
changed the title [-]Incorrect "Object is possibly 'null'" warning in typescript@next with "complex" if [/-][+]False negatives and false positives for "Object is possibly 'null'" warning in typescript@next with "complex" if [/+]on Aug 8, 2016
Activity
[-]Incorrect "Object is possibly 'null'" warning in typescript@next with "complex" if [/-][+]False negatives and false positives for "Object is possibly 'null'" warning in typescript@next with "complex" if [/+]yortus commentedon Aug 8, 2016
This also works:
ahejlsberg commentedon Aug 8, 2016
Duplicate of #10169, fix available in #10188.