Skip to content

TypeScript 3.7.5 error in the strict mode inside addQueryParams method #22

Closed
@genaby

Description

@genaby
private addQueryParams(query: object): string {
    const keys = Object.keys(query);
    return keys.length ? (
      '?' +
      keys.reduce((paramsArray, param) => [
        ...paramsArray,
        param + '=' + encodeURIComponent(query[param])
      ], []).join('&')
    ) : ''
  }

src/templates/client.mustache

(parameter) param: string
No overload matches this call.
Overload 1 of 3, '(callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string', gave the following error.
Type 'string[]' is not assignable to type 'string'.
Overload 2 of 3, '(callbackfn: (previousValue: never[], currentValue: string, currentIndex: number, array: string[]) => never[], initialValue: never[]): never[]', gave the following error.
Type 'string[]' is not assignable to type 'never[]'.
Type 'string' is not assignable to type 'never'.ts(2769)
lib.es5.d.ts(1350, 24): The expected type comes from the return type of this signature.
lib.es5.d.ts(1356, 27): The expected type comes from the return type of this signature.

I suggest following code:

  private addQueryParams(query: {[key:string]:string|number|boolean}): string {
    const keys = Object.keys(query);
    return keys.length === 0 ? ''
      : '?' + keys.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(query[key])).join('&')
  }

Metadata

Metadata

Assignees

Labels

invalidThis doesn't seem right

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions