-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Array.prototype.at
#45512
Comments
I don't see how this is related to Also, please note that there is an issue template for library changes: lib_change.md |
Being in a codebase that would produce hundreds of compile errors when I notice #36554 exists, should I merge my issue into there? |
You can always just add |
👎 For me, using |
The biggest thing of Array.prototype.at is the possibility to use negative indexes. |
For impacts: I went to use .at() today only to discover it's not supported yet. |
@Ethan-G not sure if you found a workaround yet, but i ended up adding a custom // typings/lib.esnext.array.d.ts
/** @template T - Array item type */
interface Array<T> {
/**
* Takes an integer value and returns the item at that index, allowing for
* positive and negative integers. Negative integers count back from the last
* item in the array.
*
* @param {number} index - Position of the array element to be returned
* @return {T | undefined} Element in the array matching the given index.
* Returns `undefined` if the given index can not be found
*/
at(index: number): T | undefined
}
/** @template T - Array item type */
interface ReadonlyArray<T> {
at: Array<T>['at']
} docs pulled from MDN. |
I'm with |
It seems I would have to wait until es2022 lib comes out? |
I can't get ES2022 lib in my TSconfig with 4.5.3.. I'll wait for 4.6 |
Thanks to those who raised the issue in the first place! |
As for now, this functionality is supported by |
Would it be possible to change the return type of Or would this be too difficult considering that we'd need to take into account how negative indexes work? |
hum yes , maybe they will need change the way how the generic share value. 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] | undefined;
} |
I agree with @djmisterjon 's idea on new post. |
The |
I think it's working correctly. TS deduces that the type of |
@djmisterjon That's not a hack, that's the expected and desired behaviour. |
...heum ,hi, i think you don't understand the meaning of the word hack in this context. |
There is a new method being added for
Array
:at
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at
I think typing this in a way that
undefined
is returned would allow a gradual adoption of thenoUncheckedIndexedAccess
rule:The text was updated successfully, but these errors were encountered: