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

fix(types): narrow return type of first and last #160

Merged

Conversation

crishoj
Copy link
Contributor

@crishoj crishoj commented Aug 8, 2024

Summary

As with draw() in #153, we can narrow the return type for first() and last() in case the argument is known to be empty or non-empty.

image

With custom defaultValue:

image

Related issue, if any:

For any code change,

  • Related tests have been added or updated, if needed: Type-level tests added

Does this PR introduce a breaking change?

No

Calculating...

@crishoj crishoj requested a review from aleclarson as a code owner August 8, 2024 18:21
@crishoj crishoj changed the title feat(first,last): narrow return types for non-empty arguments feat(array): narrow return type of first and last Aug 8, 2024
@radashi-bot
Copy link

Benchmark Results

Name Current Baseline Change
first ▶︎ with non-empty array 4,021,536.87 ops/sec ±0.62% 5,004,789.13 ops/sec ±0.06% 🔗 🐢 -19.65%
first ▶︎ with empty array and a default value 4,496,299.53 ops/sec ±0.06% 5,043,634.55 ops/sec ±0.06% 🔗 🐢 -10.85%
last ▶︎ with valid input 3,942,475.39 ops/sec ±0.58% 4,900,688.46 ops/sec ±0.42% 🔗 🐢 -19.55%
last ▶︎ with empty list 4,259,555.08 ops/sec ±0.06% 4,646,235.66 ops/sec ±0.07% 🔗 🐢 -8.32%

Generally speaking, any regression ≥20% should be investigated if it wasn't to be expected.

@aleclarson aleclarson added this to the v12.3.0 milestone Sep 21, 2024
@aleclarson aleclarson added the better types This PR mainly improves type definitions label Sep 21, 2024
@aleclarson aleclarson changed the title feat(array): narrow return type of first and last fix(types): narrow return type of first and last Nov 9, 2024
@crishoj
Copy link
Contributor Author

crishoj commented Nov 9, 2024

There's a new requirement for PRs that introduce new features. Without this requirement met, we won't be able to merge this.

Hi @aleclarson, I have filled out next-minor.md now (d0e722b).

@aleclarson
Copy link
Member

@crishoj Sorry, I renamed this PR so it's a fix and not a feat anymore, which means you edited next-minor.md for nothing 😅

@crishoj
Copy link
Contributor Author

crishoj commented Nov 9, 2024

Oh, no worries 😃

@aleclarson aleclarson removed this from the v12.3.0 milestone Nov 10, 2024
@aleclarson aleclarson force-pushed the narrow-return-types-for-first-and-last branch from edf0da4 to e4a4e17 Compare November 10, 2024 14:16
@aleclarson
Copy link
Member

My changes have enabled first and last to return the proper type for tuples:

const val1 = first([1, 2])
//    ^? 1

const val2 = last([1, 2])
//    ^? 2

@aleclarson aleclarson merged commit 665ba72 into radashi-org:main Nov 10, 2024
8 checks passed
@radashi-bot
Copy link

A stable release 12.2.2 has been published to NPM. 🚀

To install:

See the changes

@crishoj
Copy link
Contributor Author

crishoj commented Nov 10, 2024

Neat! Inferred type for TFirst and TLast is clever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
better types This PR mainly improves type definitions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants