TypeScript error after upgrading version 4 useParams () from react-router-dom Property 'sumParams' does not exist on type '{}'
Asked Answered
F

6

76

I get a typeScript error after upgrading to version 4 Used in useParams () from react-router-dom

"typescript": "^4.0.2"

import { useParams } from 'react-router-dom';

const { sumParams } = useParams();

Property 'sumParams' does not exist on type '{}'. 

The project worked great and only after the upgrade does it throw an error

Fir answered 30/8, 2020 at 18:2 Comment(0)
F
193

useParams is generic. You need to tell typescript which params you are using by specifying the value of the generic

There are several ways to solve this

This is my favorite way

const { sumParams } = useParams<{ sumParams: string }>();

But there are a few more ways (:

OR

interface ParamTypes {
  sumParams: string;
}

Then in your Component

const { sumParams } = useParams<ParamTypes>();

OR

add any type without interface

const { sumParams } : any = useParams();

Note: that this way you will not be able to set it as a string

OR

More option for keemor:

const { sumParams } = useParams() as { 
  sumParams: string;
}
Fir answered 30/8, 2020 at 18:2 Comment(3)
do you know what changed between TS versions to cause this to become an error? I couldn't find anything in the change log that suggested this would change.Arundel
Update: I did a binary search through typescript versions on npm, and this started becoming an issue between version 4.0.0-dev.20200624 and 4.0.0-dev.20200625. You can check out the differences between those versions here: diff.intrinsic.com/typescript/4.0.0-dev.20200624/…Arundel
Thanks, this is what I did: const { sumParams } : any = useParams(); Still getting used to typescript, so far, I am just not seeing the advantage of forgoing an elegant, dynamic language like JS in favor of the convoluted TS, but maybe I will get it one day...Edwardedwardian
U
17

Another option is:

const { sumParams } = useParams() as { 
  sumParams: string;
}
Unformed answered 23/9, 2020 at 11:56 Comment(2)
hi @Unformed I gave you a vote and also quoted you in my answerFir
It is not good since you simply force a type assertion. Use generic approach of useParams<TS type here>() function. It will allow the TS compiler infer your type on the left-side.Battat
A
6

To make it function as before, just add ":any"

const { sumParams } : any = useParams();
Anabel answered 17/9, 2020 at 18:57 Comment(0)
V
2

For newer versions of the router, generic is changed from object to union of strings.

const { param1, param2 } = useParams<'param1' | 'param2'>();

Current useParams type in the react-router types looks like this:

export declare function useParams<Key extends string = string>(): Readonly<Params<Key>>;
Vastha answered 10/2, 2022 at 13:5 Comment(0)
R
1
type ParamTypes {
  sumParams: string;
}

const { sumParams } = useParams<ParamTypes>()

this would be the clean approach to take

or

const { sumParams } = useParams<{[key: string] : string}>()

with this approach, you can get as many as param you want without declaring individually

Rhinoceros answered 4/8, 2021 at 15:15 Comment(1)
This is already the second suggestion in the accepted answer.Dexterous
T
-3

You can also access the params from useRouteMatch if you already have it imported in your file

const curRoute = useRouteMatch();
// @ts-ignore
let sumParams  = curRoute.params.sumParams
Turret answered 6/1, 2022 at 17:17 Comment(1)
useRouteMatch will match the query parameters followed by the target path param, not recommend using this way.Redhead

© 2022 - 2024 — McMap. All rights reserved.