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

optimization: refactor LastArrayElement to avoid recursion #647

Conversation

ahrjarrett
Copy link
Contributor

Hello type-fest!

I was poking around the repo and noticed that LastArrayElement was using recursion to iterate until reaching the final element, which is unnecessary since TS 4.2:

Playground

Since people are paying more attention to type-level performance lately, this change seemed like a quick and reasonable PR to make.

@ootidea
Copy link

ootidea commented Jul 14, 2023

These are my opinions.

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<any> should be any, but it is unknown.

related: #635, #636

@ahrjarrett
Copy link
Contributor Author

Personally I think those should be handled in a separate PR. The purpose of this PR was solely to make the type not recursive.

Unless, do any of these represent regressions from LastArrayElement's current behavior @ootidea ?

@ahrjarrett
Copy link
Contributor Author

ahrjarrett commented Jul 15, 2023

Also while I agree with some of your opinions, others seem useful only in certain use cases.

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:

  1. 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.

  2. 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.

@sindresorhus
Copy link
Owner

I agree with @ahrjarrett. That's out of scope for this pull request.

@sindresorhus
Copy link
Owner

@ahrjarrett Would be great if you could copy-paste your comments to the relevant issues instead: #635, #636

@sindresorhus sindresorhus merged commit 3475a02 into sindresorhus:main Jul 16, 2023
8 checks passed
@ahrjarrett ahrjarrett deleted the @ahrjarrett/last-array-element-optimization branch July 17, 2023 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants