skip to Main Content

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


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

    useLoaderData('root') as [type of the object that will be return];
    

    Know more from here

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search