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

Make fc.pre an assertion function #4709

Merged
merged 1 commit into from
Feb 24, 2024
Merged

Make fc.pre an assertion function #4709

merged 1 commit into from
Feb 24, 2024

Conversation

gruhn
Copy link
Contributor

@gruhn gruhn commented Feb 17, 2024

By making fc.pre an assertion function TypeScript can narrow types based on the condition. For example:

const item: number | undefined = numberArray[randomIndex]

fc.pre(item !== undefined)

item // narrowed type: number

If fc.pre is not an assertion function, we have to do an additional (redundant) check to convince TypeScript that item is not undefined.

See: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions

I marked this change as "decline" but I'm not sure what's appropriate. There is no runtime impact and the API hasn't really changed either. The return type is technically still void.

Category:

  • ✨ Introduce new features
  • 📝 Add or update documentation
  • ✅ Add or update tests
  • 🐛 Fix a bug
  • 🏷️ Add or update types
  • ⚡️ Improve performance
  • Other(s): ...

Potential impacts:

  • Generated values
  • Shrink values
  • Performance
  • Typings
  • Other(s): ...

Sorry, something went wrong.

@gruhn gruhn requested a review from dubzzz as a code owner February 17, 2024 13:42
Copy link

codesandbox-ci bot commented Feb 17, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit b0040be:

Sandbox Source
@fast-check/examples Configuration

@@ -0,0 +1,2 @@
declined:
Copy link
Owner

Choose a reason for hiding this comment

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

Should be marked as a minor bump

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@dubzzz
Copy link
Owner

dubzzz commented Feb 17, 2024

Sounds to be a really good idea. Thanks for the PR

Copy link

codecov bot commented Feb 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (3e56c4f) 93.57% compared to head (b0040be) 93.57%.
Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4709   +/-   ##
=======================================
  Coverage   93.57%   93.57%           
=======================================
  Files         207      207           
  Lines        5011     5011           
  Branches     1367     1367           
=======================================
  Hits         4689     4689           
  Misses        322      322           
Flag Coverage Δ
unit-tests 93.57% <100.00%> (ø)
unit-tests-18.x-Linux 93.55% <100.00%> (-0.02%) ⬇️
unit-tests-20.x-Linux 93.57% <100.00%> (+0.01%) ⬆️
unit-tests-latest-Linux 93.57% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

By making `fc.pre` an assertion function TypeScript can narrow
types based on the condition. For example:

    const item: number | undefined = numberArray[randomIndex]

    fc.pre(item !== undefined)

    item // narrowed type: number

If `fc.pre` is not an assertion function, we have to do an
additional (redundant) check to convince TypeScript that `item`
is not `undefined`.

See: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
@dubzzz
Copy link
Owner

dubzzz commented Feb 20, 2024

Everything is OK on my end. I'm just waiting a little bit to merge it. I have to decide how breaking it will be... In other words : Is it a minor or major? For now, I think so but I have to be sure.

I promise you it's gonna merged soon either for next minor or for next major (currently working on packing everything for it).

@gruhn
Copy link
Contributor Author

gruhn commented Feb 20, 2024

no stress ❤️

@dubzzz dubzzz merged commit 4aa81ee into dubzzz:main Feb 24, 2024
68 of 69 checks passed
@dubzzz
Copy link
Owner

dubzzz commented Mar 4, 2024

Will be available in a few minutes in 3.16.0

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

2 participants