I am trying to destructure some data in an object and I am getting a Property XXXX does not exist on type unknown
. I am using React Router to get some data.
let {decoded, reasonTypes, submissionDetails} = useRouteLoaderData('root')
The above code triggers the error. While the below code has no problems.
let data = useRouteLoaderData('root')
decoded
, reasonTypes
, submissionDetails
are all properties that exist on the data object.
How can I resolve this error?
2
Answers
It is showing error because, the data return by the
useRouteLoaderData('root')
may not contain the data you destructuring, so you need to use assertion for this.Example:
Know more from here
First of all if you are trying to get full type safe hook you can checkout discussion on this here
If you trying to find time saving hack you can try
let data = useRouteLoaderData<any>('root')
OR
let data = useRouteLoaderData('root') as any