Skip to content
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

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,28 @@
file_id: string;
};

export function isCaption(obj: unknown): obj is Caption {
if (typeof obj !== 'object' || obj === null) {
Copy link
Member

Choose a reason for hiding this comment

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

I hate js sometimes. 😭

return false;
}

const caption = obj as Caption;

if (typeof caption.title !== 'string') {
return false;
}

if (typeof caption.language !== 'string') {
return false;
}

if (typeof caption.file_id !== 'string') {
return false;
}

return true;
}

export type JobStopData = {
job_id: string;
};
Expand All @@ -270,6 +292,32 @@
rec_id?: string;
};

export function isCallJobMetadata(obj: unknown): obj is CallJobMetadata {
if (typeof obj !== 'object' || obj === null) {
return false;
}

const metadata = obj as CallJobMetadata;

if (typeof metadata.file_id !== 'string') {
return false;
}

if (typeof metadata.post_id !== 'string') {
return false;
}

if (metadata.tr_id !== undefined && typeof metadata.tr_id !== 'string') {

Check failure on line 310 in src/types/types.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected use of undefined
Copy link
Contributor

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?

Copy link
Member

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

Copy link
Contributor Author

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

Copy link
Contributor

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') {

Check failure on line 314 in src/types/types.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected use of undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

return false;
}

return true;
}

export type CallRecordingPropsMap = {
[key: string]: CallJobMetadata;
};
Expand Down
Loading