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] .at()
fix undefined
issue
#1
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job, that's clean. Not sure if Matt will want this change, when mine was (slightly) smaller and already too much overhead! But at least yours is complete! And it looks neat too.
src/tests/array-at.ts
Outdated
function index() { | ||
return Math.random() > 0.5 ? 0 : 1; | ||
} | ||
|
||
const a = arr.at(index()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be simpler with a forced type like this?
function index() { | |
return Math.random() > 0.5 ? 0 : 1; | |
} | |
const a = arr.at(index()); | |
const index = 1 as 1 | 2; | |
const a = arr.at(index); |
Or is it done like this for "expressivity" of the tests (like saying "sometimes we don't know the type in advance" but with code)?
src/tests/array-at.ts
Outdated
function index() { | ||
return Math.random() > 0.5 ? -1 : -2; | ||
} | ||
|
||
const a = arr.at(index()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
This will return undefined regardless if
strictNullChecks
istrue
orfalse
.But this is already the behaviour of native
ReadonlyArray
.at()
.As seen here:
Also fixed an issue with index union types, and added tests for it.
Issue was being caused by how
Subtract
util type works with unions types:Also handled the cases where index number is a "float" and not an "int" correctly, based on how
.at()
handles them