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(draw): narrow return type for empty and non-empty arrays (take II) #153

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

crishoj
Copy link
Contributor

@crishoj crishoj commented Aug 6, 2024

Summary

This is a renewed attempt at narrowing the return type for draw(), so that emptiness of the array argument is taken into account.

  • For literal array arguments, return type can be determined as T or null.
  • For mutable array arguments, return type remains T|null.
  • For immutable array arguments,
    • return type remains T|null for empty arrays (not sure why)
    • return type is narrowed to T for non-empty arrays.
image

Disclosure: I am a Typescript novice and welcome your feedback.

If the approach is sound, similar overrides could be considered for

  • first
  • last

Related issue, if any:

For any code change,

  • Related tests have been added or updated, if needed

Does this PR introduce a breaking change?

No

Bundle impact

Status File Size Difference (%)
M src/random/draw.ts 161 1 +0 (+0%)

Footnotes

  1. Function size includes the import dependencies of the function.

@MarlonPassos-git
Copy link
Contributor

Good job @crishoj, now we don't have that false positive.

crishoj and others added 3 commits August 7, 2024 12:47
Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
Copy link
Contributor

@MarlonPassos-git MarlonPassos-git left a comment

Choose a reason for hiding this comment

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

Deleting that line is all ok

crishoj and others added 2 commits August 15, 2024 13:42
@radashi-bot
Copy link

radashi-bot commented Aug 15, 2024

Benchmark Results

Name Current Baseline Change
draw ▶︎ with valid input 1,068,168.13 ops/sec ±0.33% 1,088,828.86 ops/sec ±0.58% 🔗 🐢 -1.9%

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