[@types/undercore] Object Tests - Type Checks#45894
[@types/undercore] Object Tests - Type Checks#45894typescript-bot merged 13 commits intoDefinitelyTyped:masterfrom
Conversation
|
@reubenrybnik Thank you for submitting this PR! This is a live comment which I will keep updated. Code ReviewsBecause you edited one package and updated the tests (👏), I can help you merge this PR once someone else signs off on it. Status
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": 45894,
"author": "reubenrybnik",
"owners": [
"borisyankov",
"jbaldwin",
"ccurrens",
"confususs",
"jgonggrijp",
"ffflorian",
"regevbr",
"peterblazejewicz",
"reubenrybnik"
],
"dangerLevel": "ScopedAndTested",
"headCommitAbbrOid": "5562624",
"headCommitOid": "55626249833e5d8c4576a8609d4d6c3d06b81eef",
"mergeIsRequested": true,
"stalenessInDays": 0,
"lastPushDate": "2020-07-09T22:59:07.000Z",
"lastCommentDate": "2020-07-10T01:04:07.000Z",
"maintainerBlessed": false,
"reviewLink": "https://github.com/DefinitelyTyped/DefinitelyTyped/pull/45894/files",
"hasMergeConflict": false,
"authorIsOwner": true,
"isFirstContribution": false,
"popularityLevel": "Popular",
"anyPackageIsNew": false,
"packages": [
"underscore"
],
"files": [
{
"path": "types/underscore/index.d.ts",
"kind": "definition",
"package": "underscore"
},
{
"path": "types/underscore/underscore-tests.ts",
"kind": "test",
"package": "underscore"
}
],
"hasDismissedReview": false,
"ciResult": "pass",
"lastReviewDate": "2020-07-09T23:13:05.000Z",
"reviewersWithStaleReviews": [],
"approvalFlags": 2,
"isChangesRequested": false
} |
|
🔔 @borisyankov @jbaldwin @ccurrens @confususs @jgonggrijp @ffflorian @regevbr @peterblazejewicz - please review this PR in the next few days. Be sure to explicitly select |
|
👋 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 📊
It looks like nothing changed too much. I won’t post performance data again unless it gets worse. |
jgonggrijp
left a comment
There was a problem hiding this comment.
Explicit verdict later because I want to scrutinize all the changed comments in more detail. In the meanwhile, here are some comments. I can already say that I think the updated types and tests are an improvement.
Co-authored-by: Julian Gonggrijp <dev@juliangonggrijp.com>
|
@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? |
…rer that this function is not evaluating the result using only a straight-up equality comparison.
…her small adjustments.
jgonggrijp
left a comment
There was a problem hiding this comment.
I checked the comments in details, as promised before, and I'm happy. 👍
|
Yay, thanks @jgonggrijp! I'm super excited to have this one in place 😁 |
|
I had thought that I had gotten no new errors when trying this out in my company's solution at the beginning of this PR, but when checking it again I noticed two new errors around properties not existing on type The downside of a type guard of My primary objective in this PR was to get the |
|
I don't have a strong preference between I wonder though, how about |
|
I appreciate the idea @jgonggrijp! It doesn't work as written, but there might be something similar that can be done, so I'll take a bit of time to see if I can find or figure a similar solution here. If |
|
Hmm, actually too now that I think about it I'm not sure it would be possible for the type guard to avoid transforming As another possibility here, maybe I can do something clever with overloads and a |
|
Trying it out quick, the below pair of overloads referencing all base types seems to work as desired, but I don't know how to arrive at the union below without actually referring directly to all of the types. isObject(object: undefined | null | boolean | string | number | bigint | symbol | object): object is object;
isObject(object: any): boolean;If we can't think of anything by tomorrow morning, I'll just put |
|
Unfortunately I wasn't able to think of any additional cleverness here, and thinking about the above overload solution I mentioned more I'm not even sure why that seems to work since I would expect an I've reverted back to boolean for now, but if you have any other thoughts here I'd be happy to give them a go. |
|
Maybe this is a situation that calls for isObject(object: unknown): object is object;
isObject(object: any): boolean;Although this might never fall through to the isObject(object: any): object is { [k: keyof any]: any } | Function;I'm not sure whether the |
|
That's a great idea @jgonggrijp! Unfortunately I don't think it's possible to designate all possible key types for an object (which is really really surprising to me) but |
|
It did get a little weird in the case of primitives since an object type guard yields never but this yields stuff like Trying to include |
|
Although if (_.isObject(something)) {
if (_.isFunction(something)) {
return something();
} else {
return _.keys(something);
} else {
return something;
}While this code could be reordered, expecting authors of such code to do so might be a bit unreasonable. But maybe this works without |
…object as recommended by @jgonggrijp.
…om a Dictionary<any> & object.
|
I tried out using I also tried it on a variable that was originally declared as a |
jgonggrijp
left a comment
There was a problem hiding this comment.
No I'm perfectly happy with this. Awesome!
|
@reubenrybnik Everything looks good here. Great job! I am ready to merge this PR on your behalf. If you'd like that to happen, please post a comment saying:
and I'll merge this PR almost instantly. Thanks for helping out! ❤️ |
|
Ready to merge |
|
I just published |
…pe Checks by @reubenrybnik * Updating Underscore type check functions and adding tests for them. * Removing existing tests that partially test this family of functions. * Using a never-type variable to get rid of unnecessary type union results in type guard checks. * Adding a test that checks the more common use case for isUndefined. * Update comment for isMatch to be more precise. Co-authored-by: Julian Gonggrijp <dev@juliangonggrijp.com> * Updating isEmpty documentation as recommended by @jgonggrijp. * Applying backticks more consistently in UnderscoreStatic.is* * Adjusting the returns statements of isEqual functions to make it clearer that this function is not evaluating the result using only a straight-up equality comparison. * Applying 2e8e1fe to Underscore and _Chain as well and making a few other small adjustments. * Changing isObject back to not being a type guard. * Adding a missing semicolon. * Updating isObject to use a type guard that is a cross betwen any and object as recommended by @jgonggrijp. * Also adding a test that shows that it's possible to get a function from a Dictionary<any> & object. Co-authored-by: Julian Gonggrijp <dev@juliangonggrijp.com>
…pe Checks by @reubenrybnik * Updating Underscore type check functions and adding tests for them. * Removing existing tests that partially test this family of functions. * Using a never-type variable to get rid of unnecessary type union results in type guard checks. * Adding a test that checks the more common use case for isUndefined. * Update comment for isMatch to be more precise. Co-authored-by: Julian Gonggrijp <dev@juliangonggrijp.com> * Updating isEmpty documentation as recommended by @jgonggrijp. * Applying backticks more consistently in UnderscoreStatic.is* * Adjusting the returns statements of isEqual functions to make it clearer that this function is not evaluating the result using only a straight-up equality comparison. * Applying 2e8e1fe to Underscore and _Chain as well and making a few other small adjustments. * Changing isObject back to not being a type guard. * Adding a missing semicolon. * Updating isObject to use a type guard that is a cross betwen any and object as recommended by @jgonggrijp. * Also adding a test that shows that it's possible to get a function from a Dictionary<any> & object. Co-authored-by: Julian Gonggrijp <dev@juliangonggrijp.com>
npm test.)npm run lint package-name(ortscif notslint.jsonis present).This PR makes the following changes to the is* family of functions.
UnderscoreStaticisObject,isNull, andisUndefinedfunctions to be type guards.UnderscoreStatic.isArray<T>because it violates guidelines in common mistakes (but I don't feel strongly about this and can add it back if folks would like).UnderscoreStaticisMatchfunctions to take a parameter._Chainfunctions to result in_ChainSingle<boolean>instead of_Chain<T>.This is a small deviation from the work I'm doing around collections and arrays. I'm making this change because a particular pain point for me is that my coworkers like to use
_.isUndefinedand_.isNulland are often puzzled and displeased when TS doesn't seem to understand the implications of using them as compared to other similar Underscore functions (it's worth noting that the negative effects of this are a lot worse in environments like ours that usestrictNullChecksthan they might be in those that don't). While I was in here, I thought it would also be nice to check over the rest of the is* family and make updates as necessary.