Skip to content

[underscore] Fixes sortBy on chained array wrapper#40417

Merged
sheetalkamat merged 4 commits intoDefinitelyTyped:masterfrom
ojhall:underscore-chained-array-sortby
Nov 18, 2019
Merged

[underscore] Fixes sortBy on chained array wrapper#40417
sheetalkamat merged 4 commits intoDefinitelyTyped:masterfrom
ojhall:underscore-chained-array-sortby

Conversation

@ojhall
Copy link
Copy Markdown
Contributor

@ojhall ojhall commented Nov 15, 2019

Please fill in this template.

  • 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).

Select one of these and delete the others:

If changing an existing definition:

Calling _([1, 2, 3]).chain() correctly returns a _Chain<number, number[]> type, due to:

interface UnderscoreStatic {
    chain<T>(obj: T[]): _Chain<T, T[]>;
}

However, a subsequent sortBy call then returns a _Chain<number, number> type, due to:

interface _Chain<T, V = T> {
    sortBy(iterator?: _.ListIterator<T, any>, context?: any): _Chain<T>;
}

As a result, _([1, 2, 3]).chain().sortBy(x => -x).value() returns a number type instead of number[], which doesn't match the implementation.

This PR sorts that out by trying to more closely match the other sortBy type specifications, specifically returning a _Chain<T, T[]>.

…round Z[] claimed to return a Z.

e.g. _.chain([1, 2, 3]).sortBy(x => -x).value() would claim to be of type number, not number[].
@ojhall ojhall requested a review from borisyankov as a code owner November 15, 2019 13:51
@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). Awaiting reviewer feedback labels Nov 15, 2019
@typescript-bot
Copy link
Copy Markdown
Contributor

typescript-bot commented Nov 15, 2019

@ojhall Thank you for submitting this PR!

🔔 @borisyankov @jbaldwin @ccurrens @confususs @jgonggrijp @ffflorian @regevbr - 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.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

That's its implementation behaviour, even if not provided with an array to begin with.
@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 #40417 diff
Batch compilation
Memory usage (MiB) 47.4 56.8 +19.8%
Type count 9174 9193 0%
Assignability cache size 1422 1422 0%
Language service
Samples taken 1832 1842 +1%
Identifiers in tests 1832 1842 +1%
getCompletionsAtPosition
    Mean duration (ms) 179.7 180.1 +0.2%
    Mean CV 12.0% 11.7%
    Worst duration (ms) 301.2 264.3 -12.2%
    Worst identifier value tap
getQuickInfoAtPosition
    Mean duration (ms) 170.4 171.2 +0.5%
    Mean CV 11.5% 11.5% -0.1%
    Worst duration (ms) 243.6 238.1 -2.3%
    Worst identifier map result

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 Nov 15, 2019
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.

I'm just asking for clarification below, but I chose "Request changes" to inform the bot that there should be another round of feedback.

* @see _.sortBy
**/
sortBy(iterator?: _.ListIterator<T, any>, context?: any): _Chain<T>;
sortBy<TSort>(iterator?: _.ListIterator<T, TSort>, context?: any): _Chain<T, T[]>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the new TSort type parameter really necessary, and if so, what for? I wonder whether the _Chain<T, T[]> return type wouldn't already be a sufficient solution by itself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right as far as I'm able to see - I cargo-culted it from the other sortBy implementation further up. I'll change that back. Thanks!

Copy link
Copy Markdown
Contributor Author

@ojhall ojhall Nov 18, 2019

Choose a reason for hiding this comment

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

My codebase is happy with the updated typings. 👍

@typescript-bot typescript-bot added Revision needed This PR needs code changes before it can be merged. and removed Awaiting reviewer feedback labels Nov 17, 2019
@typescript-bot
Copy link
Copy Markdown
Contributor

@ojhall 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. Thank you!

@typescript-bot
Copy link
Copy Markdown
Contributor

🔔 @jgonggrijp - Thanks for your review of this PR! Can you please look at the new code and update your review status if appropriate?

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.

Looks completely sensible to me now. 👍

@typescript-bot typescript-bot added Owner Approved A listed owner of this package signed off on the pull request. Merge:Express labels Nov 18, 2019
@typescript-bot
Copy link
Copy Markdown
Contributor

A definition owner has approved this PR ⭐️. A maintainer will merge this PR shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped!

@sheetalkamat sheetalkamat merged commit 81821c1 into DefinitelyTyped:master Nov 18, 2019
@typescript-bot
Copy link
Copy Markdown
Contributor

I just published @types/underscore@1.9.4 to npm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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). Revision needed This PR needs code changes before it can be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants