Skip to content
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

LastArrayElement<[T, ...U[]]> should be T | U, but it is U. #635

Closed
ootidea opened this issue Jun 14, 2023 · 1 comment · Fixed by #727
Closed

LastArrayElement<[T, ...U[]]> should be T | U, but it is U. #635

ootidea opened this issue Jun 14, 2023 · 1 comment · Fixed by #727
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@ootidea
Copy link

ootidea commented Jun 14, 2023

related: #636

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@ahrjarrett
Copy link
Contributor

@sindresorhus asked me to cross post a reply I made to a comment by @ootidea on #647 here.

Looking at this issue, I'm not sure that they're directly applicable (I agree that this issue is probably a bug), but since it might lead to other issues being created, I'll go ahead and put them here.

Please don't forget to tag me if you'd like me to discuss further, otherwise I'll probably miss it in my inbox!

@ootidea's comment:
#647 (comment)

LastArrayElement<[]> should be undefined, but it is never.
LastArrayElement<T[]> should be T | undefined, but it is T.
LastArrayElement<[...1[], 2]> should be 2, but it is 1 | 2.
LastArrayElement<[1, 2?]> should be 1 | 2, but it is never.
LastArrayElement<[1?, 2?]> should be 1 | 2 | undefined, but it is never.
LastArrayElement<[0, 1, 2?, ...3[]]> should be 1 | 2 | 3, but it is 0 | 1 | 2 | 3 | undefined.
LastArrayElement should be any, but it is unknown.

@ahrjarrett's response:
#647 (comment)

For instance, the expectation that LastArrayElement<[]> return undefined is useful only as the return type of a term-level JavaScript function that gets the last element of an array, or returns undefined if the array is empty.
My 2 issues with this:
Array.prototype.last does not exist (AFAIK). You could write a function called last that behaves the way you describe -- but you could also write a function that returns an error message if the array is empty, is throws, or does any number of things instead. Choosing undefined out of any other option feels somewhat arbitrary.
This doesn't seem to be in alignment with the design goals of type-fest, since it doesn't export (AFAIK) any term-level API. If someone tried to compose LastArrayElement with another type-level function, getting undefined would be very surprising behavior, because the only way that would happen would be if the value undefined was in the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants