Skip to content

[@types/underscore] Collection and Array Tests - Reduce and ReduceRight#45893

Merged
typescript-bot merged 9 commits intoDefinitelyTyped:masterfrom
reubenrybnik:underscore-tests-reduce-reduceRight
Jul 7, 2020
Merged

[@types/underscore] Collection and Array Tests - Reduce and ReduceRight#45893
typescript-bot merged 9 commits intoDefinitelyTyped:masterfrom
reubenrybnik:underscore-tests-reduce-reduceRight

Conversation

@reubenrybnik
Copy link
Copy Markdown
Contributor

@reubenrybnik reubenrybnik commented Jul 5, 2020

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).
  • Provide a URL to documentation or source code which provides context for the suggested changes: https://underscorejs.org/
  • Include tests for your changes

This PR continues the planned set that will together add up to #45201 and includes the following changes:

  • Adds tests for reduce and reduceRight.
  • Splits reduce and reduceRight into separate overloads for omitting and specifying a memo where the former forces the result type to include both the item type of the collection and undefined.
  • Updates Underscore and _Chain versions of reduce and reduceRight to work with both Lists and Dictionaries to partially fix Underscore collections functions should take objects #20623.

@reubenrybnik reubenrybnik requested a review from borisyankov as a code owner July 5, 2020 01:59
@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). Author is Owner The author of this PR is a listed owner of the package. labels Jul 5, 2020
@typescript-bot
Copy link
Copy Markdown
Contributor

typescript-bot commented Jul 5, 2020

@reubenrybnik Thank you for submitting this PR!

This is a live comment which I will keep updated.

Code Reviews

Because you edited one package and updated the tests (👏), I can help you merge this PR once someone else signs off on it.

Status

  • ✅ No merge conflicts
  • ✅ Continuous integration tests have passed
  • ✅ Most recent commit is approved by type definition owners or DT maintainers

All of the items on the list are green. To merge, you need to post a comment including the string "Ready to merge" to bring in your changes.


Diagnostic Information: What the bot saw about this PR
{
  "type": "info",
  "now": "-",
  "pr_number": 45893,
  "author": "reubenrybnik",
  "owners": [
    "borisyankov",
    "jbaldwin",
    "ccurrens",
    "confususs",
    "jgonggrijp",
    "ffflorian",
    "regevbr",
    "peterblazejewicz",
    "reubenrybnik"
  ],
  "dangerLevel": "ScopedAndTested",
  "headCommitAbbrOid": "d952cf3",
  "headCommitOid": "d952cf3c9669bd2a1b39f465bc3edc514d545609",
  "mergeIsRequested": true,
  "stalenessInDays": 0,
  "lastCommitDate": "2020-07-07T16:00:53.000Z",
  "lastCommentDate": "2020-07-07T21:57:41.000Z",
  "maintainerBlessed": false,
  "reviewLink": "https://github.com/DefinitelyTyped/DefinitelyTyped/pull/45893/files",
  "hasMergeConflict": false,
  "authorIsOwner": true,
  "isFirstContribution": false,
  "popularityLevel": "Popular",
  "anyPackageIsNew": false,
  "packages": [
    "underscore"
  ],
  "files": [
    {
      "filePath": "types/underscore/index.d.ts",
      "kind": "definition",
      "package": "underscore"
    },
    {
      "filePath": "types/underscore/underscore-tests.ts",
      "kind": "test",
      "package": "underscore"
    }
  ],
  "hasDismissedReview": false,
  "ciResult": "pass",
  "lastReviewDate": "2020-07-07T18:47:35.000Z",
  "reviewersWithStaleReviews": [],
  "approvalFlags": 2,
  "isChangesRequested": false
}

@typescript-bot
Copy link
Copy Markdown
Contributor

🔔 @borisyankov @jbaldwin @ccurrens @confususs @jgonggrijp @ffflorian @regevbr @peterblazejewicz - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

@reubenrybnik reubenrybnik changed the title [@types/underscore] Collection and Array Tests - Reduce and ReduceRight [@types/underscore] Collection Tests - Reduce and ReduceRight Jul 5, 2020
@reubenrybnik reubenrybnik changed the title [@types/underscore] Collection Tests - Reduce and ReduceRight [@types/underscore] Collection and Array Tests - Reduce and ReduceRight Jul 5, 2020
@typescript-bot
Copy link
Copy Markdown
Contributor

👋 Hi there! I’ve run some quick measurements against master and your PR. These metrics should help the humans reviewing this PR gauge whether it might negatively affect compile times or editor responsiveness for users who install these typings.

Let’s review the numbers, shall we?

Comparison details 📊
master #45893 diff
Batch compilation
Memory usage (MiB) 58.9 54.8 -7.0%
Type count 10350 9959 -4%
Assignability cache size 1766 1606 -9%
Language service
Samples taken 3257 4035 +24%
Identifiers in tests 3257 4035 +24%
getCompletionsAtPosition
    Mean duration (ms) 172.5 177.1 +2.7%
    Mean CV 11.2% 11.2%
    Worst duration (ms) 369.2 435.3 +17.9%
    Worst identifier _ _
getQuickInfoAtPosition
    Mean duration (ms) 166.5 169.8 +2.0%
    Mean CV 11.5% 11.4% -0.8%
    Worst duration (ms) 391.1 375.9 -3.9%
    Worst identifier _ _

It looks like nothing changed too much. I won’t post performance data again unless it gets worse.

@typescript-bot typescript-bot added the Perf: Same typescript-bot determined that this PR will not significantly impact compilation performance. label Jul 5, 2020
Copy link
Copy Markdown
Contributor

@jgonggrijp jgonggrijp left a comment

Choose a reason for hiding this comment

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

Good work, and indeed simpler than the previous PR. The semantics of reduce-without-initial-memo are a bit different, though. See comments below.

@typescript-bot typescript-bot added the Revision needed This PR needs code changes before it can be merged. label Jul 5, 2020
@typescript-bot
Copy link
Copy Markdown
Contributor

@reubenrybnik One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits or comments. If you disagree with the reviewer's comments, you can "dismiss" the review using GitHub's review UI. Thank you!

…ifferent result type to be specified or inferred, adding undefined to the posible result of reduce calls, and adding tests around union-type results.
@typescript-bot typescript-bot removed the Revision needed This PR needs code changes before it can be merged. label Jul 6, 2020
@typescript-bot
Copy link
Copy Markdown
Contributor

@jgonggrijp Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review?

…result can be specified when calling reduce with no memo.
…al memo to take no memo since a memo that is explicitly provided as undefined will still be treated as the memo value to use instead of using the first item in the collection..
… memo since the result in such cases is the memo.
@reubenrybnik
Copy link
Copy Markdown
Contributor Author

Alright @jgonggrijp, I think I've got all the feedback here addressed whenever you're ready to take another look. Thanks for catching the explicit undefined memo issue and incorrect undefined return type when a memo is provided, I appreciate it! Sorry for not doing a better job of making sure I had a good understanding of these special cases this time around.

Copy link
Copy Markdown
Contributor

@jgonggrijp jgonggrijp left a comment

Choose a reason for hiding this comment

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

No need to apologize!

I'm still a bit confused by some of your comments, but the code looks good to me, so I guess this is a go. 👍

@typescript-bot typescript-bot added the Owner Approved A listed owner of this package signed off on the pull request. label Jul 7, 2020
@typescript-bot typescript-bot added the Self Merge This PR can now be self-merged by the PR author or an owner label Jul 7, 2020
@reubenrybnik
Copy link
Copy Markdown
Contributor Author

Thanks @jgonggrijp, you definitely saved me from doing some incorrect things in this one. It took longer than it should have for me to get that an explicitly undefined memo didn't work like I expected it to and I had for a while erroneously thought that you were asking me to collapse the overloads for handling the different memo cases back down to a single overload with an optional memo, which is probably why some of my comments seemed somewhat off the mark.

Plugging the final result into my company's solution, I end up with one new error because the result of a memoless call now also includes undefined in its result, which I believe to be correct.

@jgonggrijp
Copy link
Copy Markdown
Contributor

Plugging the final result into my company's solution, I end up with one new error because the result of a memoless call now also includes undefined in its result, which I believe to be correct.

I agree, although you could try to prevent it by adding an even more specialized overload for memoless calls with an empty collection. It depends on how far you want to go; I'm fine with it either way.

@reubenrybnik
Copy link
Copy Markdown
Contributor Author

I agree, although you could try to prevent it by adding an even more specialized overload for memoless calls with an empty collection. It depends on how far you want to go; I'm fine with it either way.

I did spend a bit of time thinking about this, but while TS does have the concept of an empty array type never[], I don't think it is likely to know that that's what it has except in trivial cases. For example, in the chain case previous operations could yield an array with stuff in it in some cases and an empty array in others.

I think the more interesting overload to add here would be one that drops undefined when TS knows it doesn't have an empty array, which I guess would be represented by the type [T, ...T[]], but it seems very unlikely that folks will end up with that type naturally or know that it could be useful to assert it. It would be more useful to add this overload if TS introduced a type guard around the concept of a length check that would transform a T[] into the appropriate tuple type that represents the known good length of the collection in the truthy branch, but I don't think such a thing is in place at this time unfortunately.

@reubenrybnik
Copy link
Copy Markdown
Contributor Author

It looks like there's some discussion happening around such a type guard in microsoft/TypeScript#28837, so maybe at some point in the future it'll be more practical to add an overload like this. I can't think of any equivalent for dictionaries, though, so I think dictionaries are probably likely to forever go without.

@reubenrybnik
Copy link
Copy Markdown
Contributor Author

Ready to merge

@typescript-bot typescript-bot merged commit 2017793 into DefinitelyTyped:master Jul 7, 2020
ngbrown pushed a commit to ngbrown-forks/DefinitelyTyped that referenced this pull request Jul 11, 2020
…rray Tests - Reduce and ReduceRight by @reubenrybnik

* Updating type definitions for reduce and reduceRight and adding tests.

* Adding defaults for V in memo iterators.

* Updating a few tests to test constant memos more and omitted memos less.

* Updating reduce and reduceRight undefined memo overloads to allow a different result type to be specified or inferred, adding undefined to the posible result of reduce calls, and adding tests around union-type results.

* Making a few test changes to better illustrate the ways that a union result can be specified when calling reduce with no memo.

* Rewriting the overloads of reduce and reduceRight that take an optional memo to take no memo since a memo that is explicitly provided as undefined will still be treated as the memo value to use instead of using the first item in the collection..

* Removing the undefined type from the result for overloads that take a memo since the result in such cases is the memo.

* Breaking up tests for multiple aliases by alias.
danielrearden pushed a commit to danielrearden/DefinitelyTyped that referenced this pull request Sep 22, 2020
…rray Tests - Reduce and ReduceRight by @reubenrybnik

* Updating type definitions for reduce and reduceRight and adding tests.

* Adding defaults for V in memo iterators.

* Updating a few tests to test constant memos more and omitted memos less.

* Updating reduce and reduceRight undefined memo overloads to allow a different result type to be specified or inferred, adding undefined to the posible result of reduce calls, and adding tests around union-type results.

* Making a few test changes to better illustrate the ways that a union result can be specified when calling reduce with no memo.

* Rewriting the overloads of reduce and reduceRight that take an optional memo to take no memo since a memo that is explicitly provided as undefined will still be treated as the memo value to use instead of using the first item in the collection..

* Removing the undefined type from the result for overloads that take a memo since the result in such cases is the memo.

* Breaking up tests for multiple aliases by alias.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author is Owner The author of this PR is a listed owner of the package. Owner Approved A listed owner of this package signed off on the pull request. Perf: Same typescript-bot determined that this PR will not significantly impact compilation performance. Popular package This PR affects a popular package (as counted by NPM download counts). Self Merge This PR can now be self-merged by the PR author or an owner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Underscore collections functions should take objects

3 participants