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

feat(array): narrow return type for first and last #140

Conversation

crishoj
Copy link
Contributor

@crishoj crishoj commented Jul 27, 2024

Summary

Similarly to #139, we can narrow the return types for first() and last() based on the emptiness of the argument.

Before:

image

After:

image

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed

Does this PR introduce a breaking change?

No

@crishoj crishoj requested a review from aleclarson as a code owner July 27, 2024 20:47
@crishoj crishoj changed the title feat: narrow return type for first and last feat(array): narrow return type for first and last Jul 27, 2024
@@ -11,9 +11,11 @@
* // 0
* ```
*/
export function first<T>(array: readonly T[]): T | undefined
export function first(array: readonly []): undefined
export function first<T>(array: readonly T[]): T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't assume the length of the array type is not zero here, so return type has to be T | undefined. Or is your goal to align this with arr[0]-style access?

Copy link
Contributor

@MarlonPassos-git MarlonPassos-git Jul 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same case as #139 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, invalid for similar reasons. Suggest closing.

@aleclarson aleclarson closed this Jul 28, 2024
@aleclarson aleclarson added the invalid This doesn't seem right label Jul 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants