I'm writing a web application in ReactJS + Typescript. I've a functional component defined like below.
My problem is the following: in the props, for the property exercise, the parent component is passing an object, either initialized empty or of a certain type that I specify, Exercise. Then Typescript raises the following errors:
[ts] Property 'description' does not exist on type '{} | Exercise'
[ts] Property 'title' does not exist on type '{} | Exercise'
How could I refactor it so that if the object is indeed empty, it will use the default values, and otherwise, use the values passed?
EDIT: Added the other props that I use
type Exercise = {
description: string
id: string
muscles: string
title: string
}
type Props = {
category: string
children?: never
exercise: {} | Exercise
exercises: Array<[string, Exercise[]]>
onSelect: (id: string) => void
}
const Exercises = ({
exercises,
category,
onSelect,
exercise: {
description = 'Please select an exercise',
title = 'Welcome!'
}
}: Props) => (
<Grid container>
<Grid item sm>
{/* Some stuff going on with exercises, category and onSelect */ }
</Grid>
<Grid item sm>
<Paper>
<Typography variant="h4">{title}</Typography>
<Typography variant="subtitle1">{description}</Typography>
</Paper>
</Grid>
</Grid>
)