`Array.prototype.at` should return a more precise type when operating on a tuple
See original GitHub issueSuggestion
Extracted out of #45512
🔍 Search Terms
List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
Tuple Array.prototype.at
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
⭐ Suggestion
at
called on a tuple should work consistently with how index access on tuples works.
const tuple = [1, 2, 3] as const;
const element = tuple.at(1);
type T = typeof element; // currently `number | undefined`, ideally should be `2`
// Should also work with negatives.
const element2 = tuple.at(-1);
type U = typeof element2; // ideally should be `3`
// Compiler error or `undefined` for out of bounds numbers?
const element3 = tuple.at(100); // should be compiler error or
type V = typeof element3; // should be undefined
📃 Motivating Example
This is how tuples work when using index access.
💻 Use Cases
Preferring .at(index)
to [index]
.
Partial Hack Solution
as suggested by @djmisterjon
interface ReadonlyArray<T> {
/**
* Returns the item located at the specified index.
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
*/
at<U extends number>(index: U): this[U];
}
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:12 (3 by maintainers)
Top Results From Across the Web
Array.prototype.at() - JavaScript - MDN Web Docs
The at() method takes an integer value and returns the item at that index, allowing for positive and negative integers.
Read more >Why does the argument for Array.prototype.includes ...
I don't understand why a function ( Array.prototype.includes() ) made to check if an element is found in an array, should have knowledge...
Read more >Stage 3 Proposal: Array.prototype.at - Hacker News
So a dedicated numeric type for probability value is nothing more than a nice-to-have. ... Array.prototype.at = function (i) { return i <...
Read more >تويتر \ Wes Bos على تويتر: "Got to use TypeScript interface ...
TypeScript doesn't support the .at() method on readonly array / const ... `Array.prototype.at` should return a more precise type when operating on a...
Read more >Hacks for Creating JavaScript Arrays - freeCodeCamp
In JavaScript, there are not so many complex data types — you simply have ... The most popular method for creating arrays is...
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
You can get negative indices to work for tuples (template literals to drop the minus sign and then use the numberlike string to index into a shifted reversed tuple), but nobody would want this in the actual TS library. Still, enjoy:
⚙🛠
Playground link
@RyanCavanaugh ah yes, the theological argument for #26382