refine type of array access to non-undefined given proper for-loop header
See original GitHub issueMaybe 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:
- Created 3 years ago
- Reactions:2
- Comments:22 (13 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 refinedNonSparseArray
(orVector
) can be a nice addition to the standard type library.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).