Skip to content Skip to sidebar Skip to footer

How To Set Typescript Type For Matchpath Return Value

I'm trying to use matchPath to extract a route param from the parent container as described in https://stackoverflow.com/a/45492498/3574819 const topicMatch = matchPath(history.loc

Solution 1:

matchPath is a parameterized function that takes a generic type <P> and returns a match with match<P>. It's up to you to define P; otherwise I'm actually not sure how TypeScript determines the return type.

matchPath<{topic: "string"}>(...)

You could also create your own type if you wish, e.g.

interfaceRouteParams {
  topic: string;
}

and then do matchPath<RouteParams>(...).

Post a Comment for "How To Set Typescript Type For Matchpath Return Value"