profile
viewpoint

Ask questionsrefine type of array access to non-undefined given proper for-loop header

Maybe Hegel could check loop headers, and if the iteration variables are written properly, then it can narrow the types of array access to non-undefined.

For example, in the code

let nums: Array<number> = [0,1,2,3]

for (let i=0; i<nums.length; i++) {
  const n = nums[i] // must be a number
  useNum(n)
}

it will not go out of bounds, so there is no check needed like n != undefined && useNum(n).

Sorry for opening so many issues! There are lots of ideas. :)

JSMonk/hegel

Answer questions vkurchatkin

There is no way to avoid this check, unless we go into the realm of dependent types

useful!

Related questions

No questions were found.
Github User Rank List