Home > Software engineering >  Express - Request.query type definition is ParsedQs. Why is it recursive?
Express - Request.query type definition is ParsedQs. Why is it recursive?

Time:05-24

The type of request.query is ParsedQs which has the following definition:

interface ParsedQs {
    [key: string]: undefined
        | string
        | string[]
        | ParsedQs
        | ParsedQs[]
}

My guess for each types is the following:

  • A value is undefined when it isn't mentioned in the params.
    For instance: reading request.query.b when the params are ?a=1.

  • A value is a string when it's mentioned once in the params. For instance: reading request.query.a when the params are ?a=1.

  • A value is a string[] when it's mentioned multiple times in the params. For instance: reading request.query.a when the params are ?a=1&a=2.

But when is a value a ParsedQs itself?

CodePudding user response:

The query ?a[x]=b&a[y]=c is parsed into {"a":{"x":"b","y":"c"}}.

And ?a[x]=b&a=c is parsed into {"a":[{"x":"b"},"c"]}.

  • Related