Home > Mobile >  what is the type of URL query parameter in typescript?
what is the type of URL query parameter in typescript?

Time:11-05

I need to get the election id from the URL in Vue3, to use it as a query parameter in an axios call later in the call. My URL is "http://localhost:8080/#/requestPIN/py6lzerjwjntl4dmr9n77e6x73blk925?electionId=thirdElection"

I am trying to do:

get ID(): string | undefined {
return this.$route.query.electionId

}

which gives me a typescript error:

Type 'LocationQueryValue | LocationQueryValue[]' is not assignable to type 'string | undefined'. Type 'null' is not assignable to type 'string | undefined'.

what is the right type of the URL query parameters?

CodePudding user response:

Assert that the id is a string, then in the return type make sure to say it could potentially return undefined still.

get ID(): string | undefined {
    return this.$route.query.electionId as string;
}
  • Related