Check index array length
See original GitHub issueSearch Terms
Indexing, Array, Strict check
Suggestion
Strict check to indexing a array
Use Cases
Safety with arrays usages
Examples
const arr: House[] = .... // same as [] | House[]
arr[0] // throw error
if(arr.length > 0) {
arr[0] // OK
arr[200] // throw error
}
const arr2: [House, House] = ...
arr[0] // OK
arr[1] // OK
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code (with a flag)
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:10
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Array.prototype.length - JavaScript - MDN Web Docs
The length data property of an Array instance represents the number of elements in that array. The value is an unsigned, 32-bit integer...
Read more >How do I check in JavaScript if a value exists at a certain array ...
Conceptually, arrays in JavaScript contain array.length elements, starting with array[0] up until array[array.length - 1] . An array element with index i is ......
Read more >Understanding JavaScript Array.length Property By Practical ...
The length property of an array is an unsigned, 32-bit integer that is always numerically greater than the highest index of the array....
Read more >How to Find Array Length in Java - Javatpoint
If we talk about the logical size, the index of the array, then simply int arrayLength=arr.length-1, because the array index starts from 0....
Read more >How to check if an Array Index exists in JavaScript | bobbyhadz
Use the array's length property to check if an array index exists, e.g. if (arr.length > 5) {} . If the array has...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
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 would definitely find even just basic cases working better than nothing. At current I use a custom helper that takes an array and returns if it’s a tuple of a given length e.g.:
I use it quite a lot as I’m also using
--noUncheckedIndexedAccess
, but it’d be super nice to be builtin so I can just usearr.length === 0
rather than using a custom function that only exists for the type system (or more accurately forgetting to use it, and then having an error that value could be undefined). Having<
,<=
,>
,>=
would be even better for things likeconst [first, ...rest]
and so on.@Jamesernator
Well… Just for fun:
playground