In search of best practice for type checking when retrieving content via the Preview API and values for properties are null because they do not have a value yet.
Lets take this interface:
interface ActionProps {
type?: string | null
displayAs?: string | null
label?: string | null
caption?: string | null
url?: string | null
flow?: string | null
buttonType?: string | null
variant?: string | null
icon?: string | null
image?: ContentfulImage | null
isDisabled?: boolean
className?: string
onClick?: (event?: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void
}
How to go about this in our frontend? Whats the best practice?
#1 > Use optional chaining (?):
#2 > Or strict checking per property if value is not null?
Approach 1 does not feel elegant. Approach 2 is extensive.
And the interface looks messy.