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.

Check index array length

See original GitHub issue

Search 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:open
  • Created 3 years ago
  • Reactions:10
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
Jamesernatorcommented, Jun 6, 2021

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

function isLength<T>(arr: T[], length: 0): arr is [];
function isLength<T>(arr: T[], length: 1): arr is [T];
function isLength<T>(arr: T[], length: 2): arr is [T, T];
// etc
function isLength<T>(arr: T[], length: number): arr is T[] {
    return arr.length === length;
}

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 use arr.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 like const [first, ...rest] and so on.

4reactions
Kingwlcommented, Jun 6, 2021

@Jamesernator

Well… Just for fun: image

playground

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

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