Skip to content

[@types/underscore] Fix #36308 - underscore has bad chain typings fix only for function 'map' (#36319)#36510

Merged
RyanCavanaugh merged 4 commits intoDefinitelyTyped:masterfrom
Radullus:master
Jul 1, 2019
Merged

[@types/underscore] Fix #36308 - underscore has bad chain typings fix only for function 'map' (#36319)#36510
RyanCavanaugh merged 4 commits intoDefinitelyTyped:masterfrom
Radullus:master

Conversation

@Radullus
Copy link
Copy Markdown
Contributor

Fix typing in 'underscore' for function 'map'

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

  • Provide a URL to documentation or source code which provides context for the suggested changes: https://underscorejs.org/#map
    https://underscorejs.org/#chain

  • Increase the version number in the header if appropriate.

  • If you are making substantial changes, consider adding a tslint.json containing { "extends": "dtslint/dt.json" }.

@Radullus Radullus requested a review from borisyankov as a code owner June 28, 2019 06:45
@Radullus
Copy link
Copy Markdown
Contributor Author

@jgonggrijp @regevbr @renjfk Can you please review?

@typescript-bot typescript-bot added the Popular package This PR affects a popular package (as counted by NPM download counts). label Jun 28, 2019
@typescript-bot
Copy link
Copy Markdown
Contributor

typescript-bot commented Jun 28, 2019

@Radullus 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.

@typescript-bot
Copy link
Copy Markdown
Contributor

👋 Hi there! I’ve run some quick performance metrics against master and your PR. This is still an experiment, so don’t panic if I say something crazy! I’m still learning how to interpret these metrics.

Let’s review the numbers, shall we?

Comparison details 📊
master #36510 diff
Batch compilation
Memory usage 51492152.0 56392808.0 +9.5%
Type count 8871 8869 0.0%
Assignability cache size 1383 1372 -0.8%
Subtype cache size 525 517 -1.5%
Identity cache size 0 0
Language service
Samples taken 1742 1755 +0.7%
Identifiers in tests 1742 1755 +0.7%
getCompletionsAtPosition
    Mean duration (ms) 182.8 180.6 -1.2%
    Median duration (ms) 178.9 176.3 -1.4%
    Mean CV 16.8% 17.5% +4.6%
    Worst duration (ms) 275.5 270.0 -2.0%
    Worst identifier clone map
getQuickInfoAtPosition
    Mean duration (ms) 168.7 168.1 -0.4%
    Median duration (ms) 166.6 166.3 -0.2%
    Mean CV 16.6% 17.9% +8.3%
    Worst duration (ms) 258.8 246.8 -4.7%
    Worst identifier partial max

It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place.


If you have any questions or comments about me, you can ping @andrewbranch. Have a nice day!

@jgonggrijp
Copy link
Copy Markdown
Contributor

I defer this one to @regevbr because he is currently working on a complete fix for the chain typings. I'm not sure whether the fix in the current PR will combine well with his work, but I think the unittest would be a welcome addition in any case.

Copy link
Copy Markdown
Contributor

@regevbr regevbr left a comment

Choose a reason for hiding this comment

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

@Radullus Thanks for the PR!

The T in _Chain<T,V> represents the type of the object and V represents the object.
So for example, string[] should be _Chain<string,string[]> and _.Dictionary<number> should be _Chain<number, _.Dictionary<number>

The lines you changed made your test pass, but they would cause other tests, that should be there but are not :), to fail.

Please see the following suggestion to fix the issue:

map<TResult>(iterator: _.ListIterator<T, TResult>, context?: any): _Chain<TResult>;

should be changed to

 map<TResult>(iterator: _.ListIterator<T, TResult>, context?: any): _Chain<TResult, TResult[]>; 

map<TResult>(iterator: _.ObjectIterator<T, TResult>, context?: any): _Chain<TResult>;

should be changed to

 map<TResult>(iterator: _.ObjectIterator<T, TResult>, context?: any): _Chain<TResult, _.Dictionary<TResult>>;

Can you please try it (after reverting your change) and see if the test passes? Also I agree with @jgonggrijp that more tests are always welcome, so can you please also add a test for map operation on an object?

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

@Radullus 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!

… only for function 'map' (#36319)

Add more test and applay changes form review
@Radullus
Copy link
Copy Markdown
Contributor Author

Radullus commented Jul 1, 2019

Hi @regevbr

  • I added more test, for mapping 'Dictionary'.
  • I apply charges suggested for Line 5103 in line DefinitelyTyped/types/underscore/index.d.ts d200340
  • I modified Line 5115 DefinitelyTyped/types/underscore/index.d.ts d200340.
    Map function for 'chain' object don't return 'Dictionary' but return 'Array'. You can see some of my tested https://jsfiddle.net/Radullus/fsmj2rxa/37/

* @see _.map
**/
map<TArray>(iterator: _.ListIterator<T, TArray[]>, context?: any): _ChainOfArrays<TArray>;
map<TArray>(iterator: _.ListIterator<T, TArray>, context?: any): _ChainOfArrays<TArray>;
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.

this should be reverted as current behavior is ok

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.

done.
I have add test for this definition of 'map', but I have to comment part of the 'testing' witch check output because it will cause the test to fail.

* @see _.map
**/
map<TArray>(iterator: _.ObjectIterator<T, TArray[]>, context?: any): _ChainOfArrays<TArray>;
map<TArray>(iterator: _.ObjectIterator<T, TArray>, context?: any): _ChainOfArrays<TArray>;
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.

this should be reverted as current behavior is ok

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.

done.
I have add test for this definition of 'map', but I have to comment part of the 'testing' witch check output because it will cause the test to fail.

'fake id': { name: 'curly', age: 60 },
};

let youngPeopleId: string[] = _.chain(usersData)
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.

This test should actually fail, as performing filter on a a Dictionary returns T[], thus performing map on it will need the following iterator:

(value: T, index: number, list: T[]): TResult

The reason this test passes now is because of missing signature of filter for _Chain which should be:

filter(iterator: _.ObjectIterator<T, boolean>, context?: any): _Chain<T, T[]>;

But fixing it will not even help as the entire _Chain interface is malformed :-( and I'm working on a wide fix for it.
I would rather you only add tests that use the fixed map method

Copy link
Copy Markdown
Contributor Author

@Radullus Radullus Jul 1, 2019

Choose a reason for hiding this comment

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

You have right. I will stick in this test to 'map' function. I interface definition I was trying to test.

@regevbr
Copy link
Copy Markdown
Contributor

regevbr commented Jul 1, 2019

Thanks @Radullus!
The _Chain interface is completely messed up currently :(
I added some comments for you to review

@typescript-bot
Copy link
Copy Markdown
Contributor

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

@typescript-bot
Copy link
Copy Markdown
Contributor

Updated numbers for you here from f3f7a2f:

Comparison details 📊
master #36510 diff
Batch compilation
Memory usage 51629480.0 52528944.0 +1.7%
Type count 8871 8940 +0.8%
Assignability cache size 1383 1389 +0.4%
Subtype cache size 525 525 0.0%
Identity cache size 0 0
Language service
Samples taken 1742 1796 +3.1%
Identifiers in tests 1742 1796 +3.1%
getCompletionsAtPosition
    Mean duration (ms) 180.6 178.2 -1.4%
    Median duration (ms) 177.7 174.8 -1.6%
    Mean CV 17.1% 16.8% -1.8%
    Worst duration (ms) 278.2 260.9 -6.2%
    Worst identifier templateSettings chain
getQuickInfoAtPosition
    Mean duration (ms) 167.9 164.3 -2.2%
    Median duration (ms) 165.5 162.7 -1.7%
    Mean CV 16.9% 16.2% -4.4%
    Worst duration (ms) 238.4 233.0 -2.3%
    Worst identifier partial value

It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place.

Radullus added 2 commits July 1, 2019 13:54
… only for function 'map' (#36319)

 - Add more test
 - revert part of typings for function
@typescript-bot
Copy link
Copy Markdown
Contributor

Updated numbers for you here from c42c111:

Comparison details 📊
master #36510 diff
Batch compilation
Memory usage 51005080.0 53628304.0 +5.1%
Type count 8871 9005 +1.5%
Assignability cache size 1383 1403 +1.4%
Subtype cache size 525 551 +5.0%
Identity cache size 0 0
Language service
Samples taken 1742 1825 +4.8%
Identifiers in tests 1742 1825 +4.8%
getCompletionsAtPosition
    Mean duration (ms) 183.9 185.1 +0.7%
    Median duration (ms) 180.1 181.6 +0.8%
    Mean CV 17.1% 17.4% +1.6%
    Worst duration (ms) 271.4 287.2 +5.8%
    Worst identifier object templateSettings
getQuickInfoAtPosition
    Mean duration (ms) 169.2 168.8 -0.2%
    Median duration (ms) 167.4 166.0 -0.8%
    Mean CV 17.0% 15.9% -6.8%
    Worst duration (ms) 257.6 246.2 -4.5%
    Worst identifier a score

It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place.

.value();

// Test map function with _ChainOfArrays<>
let usersTable_2 /*: { age: number; name: string; id: string }[][]*/ = _.chain(usersData)
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.

why did you remove the type here?
It is best to use the flatten() method here and then get the value

Copy link
Copy Markdown
Contributor Author

@Radullus Radullus Jul 1, 2019

Choose a reason for hiding this comment

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

because the type by the underscore should be like this one in the comment, But with current typescript definition, There is missing one level of array nest.
{ age: number; name: string; id: string }[][] // type return by underscore
{ age: number; name: string; id: string }[] // type with is calculate base of current definitions
It would require more changes in the underscore typescript definition. And I'm not certain what should be changed?
interface _ChainOfArrays<T> extends _Chain<T[]>
to
interface _ChainOfArrays<T> extends _Chain<T[], T[][]>
it will not match with data return from function 'groupBy'

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.

You are correct. Lets leave it be for now

})
.value();

let usersTable_3 /*: { score: number; fullName: string; login: string }[][]*/ = _.chain(usersTable)
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.

same here - why did you remove the type?

@typescript-bot typescript-bot added Owner Approved A listed owner of this package signed off on the pull request. Merge:Express labels Jul 1, 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!

@RyanCavanaugh RyanCavanaugh merged commit 020e04a into DefinitelyTyped:master Jul 1, 2019
@typescript-bot
Copy link
Copy Markdown
Contributor

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

iRON5 pushed a commit to iRON5/DefinitelyTyped that referenced this pull request Aug 13, 2019
…ain typi… (DefinitelyTyped#36510)

* [@types/underscore] Fix DefinitelyTyped#36308 - underscore has bad chain typings fix only for function 'map' (DefinitelyTyped#36319)

* [@types/underscore] Fix DefinitelyTyped#36308 - underscore has bad chain typings fix only for function 'map' (DefinitelyTyped#36319)
Add more test and applay changes form review

* [@types/underscore] Fix DefinitelyTyped#36308 - underscore has bad chain typings fix only for function 'map' (DefinitelyTyped#36319)
 - Add more test
 - revert part of typings for function

* Comment part of test with currently didn't pass
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. 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.

5 participants