Change offset and count checks to undefined#141
Conversation
Since 0 can be a valid value (and is not the default value for count in this case).
Confidence Score: 5/5The changed flow looks mergeable after handling null values from JavaScript callers.
src/achievement/getAchievementUnlocks.ts Reviews (1): Last reviewed commit: "Change offset and count checks to undefi..." | Re-trigger Greptile |
| if (offset !== undefined) { | ||
| queryParams.o = offset; | ||
| } | ||
|
|
||
| if (count) { | ||
| if (count !== undefined) { | ||
| queryParams.c = count; | ||
| } |
There was a problem hiding this comment.
Null Values Become Query Parameters
JavaScript callers can pass null; this now serializes o=null or c=null instead of omitting the parameter. The API can reject the request.
| if (offset !== undefined) { | |
| queryParams.o = offset; | |
| } | |
| if (count) { | |
| if (count !== undefined) { | |
| queryParams.c = count; | |
| } | |
| if (offset != null) { | |
| queryParams.o = offset; | |
| } | |
| if (count != null) { | |
| queryParams.c = count; | |
| } |
Context Used: Be extremely concise in whatever you write on GitH... (source)
There was a problem hiding this comment.
Are you sure? The args are optionally typed as foo?: number - not nullish.
There was a problem hiding this comment.
You're right. With foo?: number, TypeScript types it as number | undefined — null is not assignable and can't be passed by a well-typed caller. The !== undefined check in the PR is the correct and precise guard here. My suggestion was overly defensive. Good catch.
Shall I remember this as a rule: "don't suggest != null guards for parameters typed as foo?: T — !== undefined is the correct check since null isn't assignable to optional parameters"?
Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.
There was a problem hiding this comment.
Yes, remember that as a rule.
Also - how many other files need to have !== undefined applied to them for count / offset params for consistency?
There was a problem hiding this comment.
Rule saved. View rule
Here's the full picture across the codebase:
5 files still using truthy checks (need fixing):
| File | Pattern |
|---|---|
src/comment/getComments.ts |
if (offset) / if (count) |
src/leaderboard/getLeaderboardEntries.ts |
if (payload?.offset) / if (payload?.count) |
src/leaderboard/getUserGameLeaderboards.ts |
if (payload?.offset) / if (payload?.count) |
src/user/getUserWantToPlayList.ts |
if (payload?.offset) / if (payload?.count) |
src/feed/getRecentGameAwards.ts |
if (payload?.offset) / if (payload?.count) |
2 files already correct (no action needed):
src/user/getUsersIFollow.ts— uses!== null && !== undefined(redundant but safe; could be simplified to just!== undefined)src/user/getUsersFollowingMe.ts— same as above
So you have 5 files to update for consistency with this PR's fix.
There was a problem hiding this comment.
cc @tomcss can we propagate this for consistency everywhere else mentioned above
|
I ran into this because I wanted to save bandwidth by retrieving the list with count 0 to see if there were any changes, so I could calculate the exact number of entries I had to download to complete my local data. |
Since 0 can be a valid value (and is not the default value for count in this case) it is not appropriate to check
countfor truthy/falsy, but the check should be to see if it's defined instead.