In TypeScript, for a property that could be null / undefined / number, we need to check if the property exists first before performing a comparison on it, like this:
const obj: { size: number | null } = { size: 123 /* hardcoded for example */ };
if (obj.size && obj.size > 0) {
console.log();
}
But this gets flagged by explicit-length-check and autofixed to:
const obj: { size: number | null } = { size: 123 /* hardcoded for example */ };
if (obj.size > 0 && obj.size > 0) {
console.log();
}
Which hits the original TypeScript error:
'obj.size' is possibly 'null'.ts(18047)
Some fix ideas:
obj.size && obj.size > 0
on the assumption that the first part of the expression is a presence check and that it would it would be redundant to autofix the first part of the expression to another comparison.Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too