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

Improved flow type #12

Merged
merged 4 commits into from
May 15, 2017
Merged

Improved flow type #12

merged 4 commits into from
May 15, 2017

Conversation

alexreardon
Copy link
Owner

hardening up the flow types so that the memoized function is the same type as the function passed in

@codecov-io
Copy link

codecov-io commented May 12, 2017

Codecov Report

Merging #12 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #12   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           1      1           
  Lines          15     16    +1     
=====================================
+ Hits           15     16    +1
Impacted Files Coverage Δ
src/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5cb7ffc...14e875c. Read the comment docs.

src/index.js Outdated
let lastThis: any;
let lastArgs: Array<any> = [];
let lastResult: any;
export default function <ResultFn: Function>(resultFn: ResultFn, isEqual?: EqualityFn = simpleIsEqual): ResultFn {
Copy link
Owner Author

@alexreardon alexreardon May 12, 2017

Choose a reason for hiding this comment

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

@thejameskyle I was unable to get a better check than:

<ResultFn: Function>

I tried to use this:

<ResultFn: () => mixed>

but flow complained when I tried to memoize functions that had arguments. I also tried this with no success.

<ResultFn: (...args: Array<mixed>)>
ResultFn: (...args: Array<mixed>) => mixed

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, weird... that used to work. Maybe recent changes to arity have changed that.

You could make it a little bit stronger by using:

(...Array<any>) => mixed

@alexreardon alexreardon force-pushed the improved-flow-typing branch from 9a3a2ca to 746309b Compare May 14, 2017 10:59
@alexreardon alexreardon force-pushed the improved-flow-typing branch from 746309b to d219433 Compare May 14, 2017 21:59
.flowconfig Outdated
@@ -6,6 +6,8 @@

[options]

experimental.strict_call_arity=true
Copy link
Owner Author

Choose a reason for hiding this comment

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

I had added this also to ensure that memoizeOne will work with the upcoming flow version

src/index.js Outdated
let lastThis: any;
let lastArgs: Array<any> = [];
let lastResult: any;
export default function <ResultFn: (...Array<any>) => mixed>(resultFn: ResultFn, isEqual?: EqualityFn = simpleIsEqual): ResultFn {
Copy link
Owner Author

Choose a reason for hiding this comment

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

@thejameskyle <ResultFn: (...Array<any>) => mixed> seemed to work fine. I am still a little confused as to what this is communicating. Here is my current understanding:

ResultFn is a type of function (as communicated by the syntax() => {}) that can accept any length of arguments ((...Array<any>)) and returns a mixed (which means that you must check the result type before you can do type specific operations on it).

Is that correct?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, that's right. While you're not enforcing inputs, you can at least be sure that you aren't handling the output incorrectly.

@alexreardon
Copy link
Owner Author

This PR brings the flow coverage up from 64% to 90.6%

@alexreardon alexreardon merged commit bdd0e37 into master May 15, 2017
@alexreardon alexreardon deleted the improved-flow-typing branch March 21, 2018 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants