-
Notifications
You must be signed in to change notification settings - Fork 9
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
fix see more #464
fix see more #464
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.
A few suggestions
// Sometimes string fields are returned as lists. This can happen when there are duplicate entries in Coda | ||
const head = (item: string | string[]) => { | ||
const head = (item: any | any[]) => { |
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 believe it would be more type-safe down the line to use unknown
here. This change doesn't seem to break anything in the existing code and so should be easy to apply now.
const head = (item: any | any[]) => { | |
const head = (item: unknown | unknown[]) => { |
@@ -132,7 +132,7 @@ export type AnswersRow = CodaRowCommon & { | |||
Banners: '' | Entity[] | |||
'Rich Text': string | |||
Subtitle?: string | |||
Icon?: string | |||
Icon?: Entity | string | Entity[] |
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 type change doesn't seem necessary as far as I can tell. The new extractIcon
function returns string | undefined
, so I think that is a suitable type here.
Icon?: Entity | string | Entity[] | |
Icon?: 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.
this is here mainly to document what Coda returns. Which can be either the raw image (base64 encoded), an url to the image, a reference to a Coda item or a list of references to Coda items... I'm not sure what exactly causes what
const item = head(val) | ||
if (typeof item === 'string') return extractText(val as string) | ||
if (item) return (item as Entity).url | ||
return undefined | ||
} | ||
|
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 think it would be nice to avoid the val as
casts to benefit from type checking of val
in the future. I've suggested an isStringOrStringArray
type guard to do this, though there might be a more elegant way.
const item = head(val) | |
if (typeof item === 'string') return extractText(val as string) | |
if (item) return (item as Entity).url | |
return undefined | |
} | |
if (isStringOrStringArray(val)) return extractText(val) | |
const item = head(val) | |
if (item) return item.url | |
} | |
const isStringOrStringArray = (x: unknown): x is string | string[] => { | |
const item = head(x) | |
return typeof item === 'string' | |
} | |
I also think the last return undefined
isn't necessary. I think undefined
will be returned if none of the if
cases are matched, even without this line.
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 thing is that I know that if it's not a string and not undefined, then it's going to be an Entity
at this point. Typescript doesn't know that.
Wouldn't that be void
?
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 thing is that I know that if it's not a string and not undefined, then it's going to be an Entity at this point. Typescript doesn't know that.
Ah maybe part of the confusion here is that in my local code where I was trying this out, I also changed the signature of the head
function to const head = <T,>(item: T | T[]): T
. With this, I'm pretty sure that typescript does know that item
is an Entity
at that point. You can see in screenshot below that my local compiler is showing the type of item
as that point as Entity
.
The way I'm reasoning about it is that the parameter type is undefined | string | string[] | Entity | Entity[]
and then:
if (!val) return val
removes the possibility ofundefined
if (isStringOrStringArray(val)) return extractText(val)
removes the possibility ofstring | string[]
head
(with the signature change above) narrowsEntity | Entity[]
toEntity
At any rate, I'll make a PR at some point to improve the head
function so that it's easy to make future code more type safe 😊 . Probably less important getting the UI redesign out.
Wouldn't that be void?
No, I don't think so. I think void
is a typescript type but the actual return type is undefined
. I wrote this JS fiddle as a quick test: https://jsfiddle.net/#&togetherjs=M9fHnDlskK
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.
ohhh, I like that version of head!
No description provided.