Skip to content

Commit

Permalink
Fix the TypeScript types for .stringify() and .stringifyUrl() (#279)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
esetnik and sindresorhus authored Sep 28, 2020
1 parent eb769d2 commit 38906bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 10 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,18 @@ export interface StringifyOptions {
readonly skipEmptyString?: boolean;
}

export type Stringifiable = string | boolean | number;

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

/**
Stringify an object into a query string and sort the keys.
*/
export function stringify(
object: {[key: string]: any},
object: StringifiableRecord,
options?: StringifyOptions
): string;

Expand All @@ -358,9 +365,9 @@ export interface UrlObject {
readonly url: string;

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

/**
Overrides the fragment identifier in the `url` property.
Expand Down
17 changes: 16 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,19 @@ expectType<queryString.ParsedUrl>(
// Extract
expectType<string>(queryString.extract('http://foo.bar/?abc=def&hij=klm'));

expectType<string>(queryString.stringifyUrl({url: 'https://sindresorhus.com', query: {foo: undefined}}));
expectType<string>(
queryString.stringifyUrl({
url: 'https://sindresorhus.com',
query: {
fooArray: [
'a',
'b'
],
fooNumber: 1,
fooBoolean: true,
fooNull: null,
fooUndefined: undefined,
fooString: 'hi'
},
})
);

0 comments on commit 38906bc

Please sign in to comment.