Skip to content

stringifyUrl type should accept string[] to be consistent with parse #278

@esetnik

Description

@esetnik

Example

  const queryStringParams = queryString.parse(search);
  const sourceInjectedQueryString = queryString.stringifyUrl({
    url: source,
    query: queryStringParams,
  });

Error

[tsserver 2322] [E] Type 'ParsedQuery<string>' is not assignable to type 'Record<string, string | null | undefined>'.
  Index signatures are incompatible.
    Type 'string | string[] | null' is not assignable to type 'string | null | undefined'.
      Type 'string[]' is not assignable to type 'string'.

This is a regression introduced by b15f945

ParsedQuery type has array

export interface ParsedQuery<T = string> {
	[key: string]: T | T[] | null;
}

UrlObject type does not support an array

export interface UrlObject {
	readonly url: string;

	/**
	Qverrides queries in the `url` property.
	*/
	readonly query: Record<string, string | undefined | null>;

	/**
	Overrides the fragment identifier in the `url` property.
	*/
	readonly fragmentIdentifier?: string;
}

Note

Additionally it should support number and boolean since these are also stringifiable.

The type of object argument in stringify should also match the type of query in stringifyUrl as they both stringify a record.

Proposed Fix

export type Stringifiable = string | boolean | number;
export type StringifiableRecord = Record<
  string,
  Stringifiable | Stringifiable[] | null | undefined
>;

export interface UrlObject {
  readonly url: string;

  /**
	Qverrides queries in the `url` property.
	*/
  readonly query: StringifiableRecord;

  /**
	Overrides the fragment identifier in the `url` property.
	*/
  readonly fragmentIdentifier?: string;
}

export function stringify(
  object: StringifiableRecord,
  options?: StringifyOptions
): string;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions