H
H
hinie2022-04-11 21:48:52
Vue.js
hinie, 2022-04-11 21:48:52

How to specify type in vue Router?

Hello, I'm using typescript in a project and I need to compare a static value of type number with the
id == this.$route.query.id//id passed through the router exactly number and this.$route.query.id should also be considered a number, but for some reason it's not...
But typescript

This condition will always return 'false' since the types 'number' and 'LocationQueryValue | LocationQueryValue[]' have no overlap.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
standbyoneself, 2022-04-18
@standbyoneself

LocationQueryValue is a type alias for string | null. You must check that the identifier from the query parameters is not null, in which case cast it to a number using the String constructor.
String('1') === 1 // true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question