question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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. 😃

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:22 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
peter-leonovcommented, May 22, 2020

Would it be useful to have two inbuilt types Array and SparseArray to represent this ?

I also like the idea of having two distinct types here. Following the rule of the least surprise I’d say that the Array type should stay sparse by default and a more refined NonSparseArray (or Vector) can be a nice addition to the standard type library.

2reactions
trusktrcommented, Jun 6, 2020

I had no idea forEach skipped holes. 😆 That’s neat! After all these years, it never mattered (or I wasn’t aware that it mattered).

Read more comments on GitHub >

github_iconTop Results From Across the Web

for-loop causes Cannot set property '0' of undefined
Use Array#map instead of for . private renderSelectedSite(){ const features = (new GeoJSON()).readFeatures(this.selectedSite.geometry); let ...
Read more >
5. Working with Arrays and Loops - JavaScript Cookbook [Book]
A for loop can be used to access every element of an array. The array begins at zero, and the array property length...
Read more >
Google JavaScript Style Guide
1 Introduction. This document serves as the complete definition of Google's coding standards for source code in the JavaScript programming language.
Read more >
Safari Technology Preview Release Notes - Apple Developer
Safari Technology Preview Release Notes. Release 160. Note: Shared Tab Groups and syncing for Tab Groups, Website Settings, and Web Extensions are not...
Read more >
7. The WhyML Language Reference - Why3
Thus, a snapshot array a of type {array int} can be read from ( a[42] is ... not refer to the program point...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found