Vue-router: Type declaration incomplete for Location.query

Created on 8 Dec 2017  路  13Comments  路  Source: vuejs/vue-router

Version

2.7.0

Reproduction link

https://jsfiddle.net/50wL7mdz/82013/ (This doesn't actually work, but shows the code you'd need locally to repro the bug)

Steps to reproduce

  1. Note that JSFiddle doesn't appear to give Typescript compiler errors, so the repro linked will have to be ported into a local Typescript Vue project.

  2. In a Typescript Vue project:

router.push({ query: { array: ["1", "2", "3"] }})

What is expected?

This should compile without error

What is actually happening?

You get an error:

 error TS2345: Argument of type '{ name: string; query: QueryParams; }' is not assignable to parameter of type 'RawLocation'.
  Type '{ name: string; query: QueryParams; }' is not assignable to type 'Location'.
    Types of property 'query' are incompatible.
      Type 'QueryParams' is not assignable to type 'Dictionary<string> | undefined'.
        Type 'QueryParams' is not assignable to type 'Dictionary<string>'.
          Index signatures are incompatible.
            Type 'string | string[] | undefined' is not assignable to type 'string'.
              Type 'undefined' is not assignable to type 'string'.

Most helpful comment

@posva Any updates? When will there be a new release?

All 13 comments

@ktsn I couldn't find anything about Type 'string | string[] | undefined' is not assignable to type 'string' but that line doesn't make sense to me. Do you know this error?

@posva No, I don't. In any cases, the snippet looks invalid as a repro since query won't accept number[] - the error should be Type 'number[]' is not assignable to type 'string'.
I suspect the error caused by the other place where QueryParams is used in OP's code.

It's weird I was getting the same output but I don't anymore 馃槅
I was wondering if we should be more permissive about the query string because the user is able to provide custom query parsing (https://router.vuejs.org/en/api/options.html#parsequery--stringifyquery). I don't know if there's a smarter way to handle this though

@ktsn @posva I have made 2 updates to the OP (I apologize the original bug report I posted was not completely correct):

  • I should have had strings in the repro not numbers. The repro doesn't work in JSFiddle (I'm not sure why, I assumed that JSFiddle ignores Typescript compilation errors). Locally this expression fails to compile for me.
  • The vue-router version I use locally is 2.7.0 (based on the source code annotation I don't think 3.0.1 fixes my problem though).

I think the line that would cause my problem is that the type declaration https://github.com/vuejs/vue-router/blob/dev/types/router.d.ts#L110 is query?: Dictionary<string> when in fact vue-router also accepts query parameters that are string[] (this is true _even if the user doesn't specify custom query parsing_). This feature seems to be undocumented though.

You can verify that vue-router accepts for query values a string[] by visiting @posva 's JSFiddle at https://jsfiddle.net/posva/gnk3a044/1/ .

I have same issue, string[] works as expected but current typings won't let me use it.

This can be closed now with https://github.com/vuejs/vue-router/pull/2050 merged.

thanks, I didn't see I wasn't closed

@posva @zigomir I am getting the same warning with params and named route.
It works, but in my IDE I get the annoying warning:

Argument type {name:string, params:{"0":string, status:string, msg:string}} is not assignable to parameter type RawLocation.

Why? I mean why it works when compiled, but i get the warning message in my console?

It hasn't been released yet, it will be soon 馃檪

@posva Any updates? When will there be a new release?

Getting this on vue-router 3.0.2...

I'm wondering how to resolve this as well? Any news?

Since query should be Dictionary<string>,Here is my soluction and works fine for me:

private editArticel(id: string) {
    const otherQuery: Dictionary<string> = {}
    otherQuery.id = id
    this.$router.push({
      path: './editArticle',
      query: otherQuery
    })
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

druppy picture druppy  路  3Comments

sqal picture sqal  路  3Comments

gil0mendes picture gil0mendes  路  3Comments

shinygang picture shinygang  路  3Comments

saman picture saman  路  3Comments