Is there way to type route.params in Vue.js?

Viewed 980

I ran into a trouble while Typescriptizing my vue.js app. I try to pluck a parameter from route.params and assign it to a property of a reactive form. However, TypeScript gave me a warning that the type of parameter must be string|string[], so it can't be assigned to a string property. The only solution I came up with is the following :

form.productId = route.params.productId as string

I think it would be a better solution if I could type parameters of route.params, but I have no idea of how to do it. I would appreciate it if someone can help on this.

1 Answers

The route.params are already typed, which is why you're seeing the warning.

Their type is string | string[], because a route can have a param multiple times, which internally gets transformed into an array of strings.

So the proper solution is the one you're already using: tell TypeScript: "this route param will never be an array of strings. Trust me on this one, I know what I'm doing. Treat it as string.

Related