-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add guards for types needed for prop types by mobile #44
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks simple enough but having done similar stuff recently I am wondering if we have a plan to potentially generate this sort of code? I understand there are some tools out there that do it.
return false; | ||
} | ||
|
||
if (metadata.tr_id !== undefined && typeof metadata.tr_id !== 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the undefined
check necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason to use undefined
is because tr_id
is optional. So what we are doing here is saying...
- Is there a value on
tr_id
? (tr_id !== undefined
) - If so, is the value a string? (
typeof tr_id === 'string'
)
If we don't check for undefined, we have the following things that may happen:
- Only check
typeof tr_id === 'string'
- An object that doesn't have defined
tr_id
won't pass the check
- An object that doesn't have defined
tr_id && typeof tr_id === 'string'
- An object that
tr_id
is a 0, a null or any other falsy value would be considered valid. This probably have no implications, since you usually check whether it is defined by checking whether the value is truthy or falsy, but this direction is more correct.
- An object that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Could we add exceptions to make the linter happy?
return false; | ||
} | ||
|
||
if (metadata.rec_id !== undefined && typeof metadata.rec_id !== 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
@@ -259,6 +259,28 @@ export type Caption = { | |||
file_id: string; | |||
}; | |||
|
|||
export function isCaption(obj: unknown): obj is Caption { | |||
if (typeof obj !== 'object' || obj === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hate js sometimes. 😭
Not AFAIK, but for what is worth, after doing a few for the webapp by hand, I discovered that our MM copilot does a great job with these 😅 |
@larkox May need to rebuild and push (hopefully it's enough). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Summary
For prop validation, we need guards for several types. This PR add those guards.
Ticket Link
NONE