-
Notifications
You must be signed in to change notification settings - Fork 27
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
feat(draw): add overloads for empty and non-empty arrays #139
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.
Hey, thanks for the contribution!
readonly T[]
can represent an empty array, so we can't assume a return type of T
As I understand the rule of overload ordering, the first applicable overload is chosen by TS: Thus,
I am far from a TS expert though, so do correct me if I'm wrong. |
There are some cases where your typing doesn't work, look at this code: const list: number[] = []
const randonNumber = draw(list) This could create a false positive. I prefer that the types indicates it can return const list: number[] = [1, 2]
const randonNumber = draw(list)! Therefore, we wouldn't change the typing, but perhaps we could add a line in the documentation explaining the use of |
@MarlonPassos-git thanks for the counter-example. I had only checked the overloads with literal arguments. I tried writing a conditional type, but didn't get it to work. Suggest closing, thanks for the careful review 🙏 |
Unfortunately, typescript has some limitations, including this one. When the first version of the documentation is available, we can write an alert about it. |
@MarlonPassos-git The |
@aleclarson
I have 2 choices:
I, personally, prefer the second option. But currently all the documentation is for function modules. What do you think about this? |
@MarlonPassos-git |
Summary
The return type of
draw()
does not take emptiness of the array argument into account.It would be helpful to assert that:
null
is returned when array argument is emptyT
is returned otherwiseBefore:
After:
For any code change,
Does this PR introduce a breaking change?
No
Bundle impact
Calculating...