-
Notifications
You must be signed in to change notification settings - Fork 124
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
[ReadonlyArray] Add types for .at()
#101
base: main
Are you sure you want to change the base?
Conversation
One case remain unhandled, and I'd love some help on it: With const arr = [false, 1, '2'] as const;
const index: number = 1
const a = arr.at(index)
// ^? const a: false | 1 | "2" |
Did a PR: Sheraff#1 This will return undefined regardless if But this is already the behaviour of native As seen here: interface Array<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(index: number): T | undefined;
}
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(index: number): T | undefined;
} Also fixed an issue with index union types, and added tests for it. Issue was being caused by how type Foo = Subtract<10, 1 | 2>
// Foo should give `9 | 8` but gives `9` only Also handled the cases where index number is a "float" and not an "int" correctly, based on how |
[ReadonlyArray] `.at()` fix `undefined` issue
This PR solves #100
See issue for a more complete description