Skip to content

Commit 38906bc

Browse files
esetniksindresorhus
andauthoredSep 28, 2020
Fix the TypeScript types for .stringify() and .stringifyUrl() (#279)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent eb769d2 commit 38906bc

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed
 

‎index.d.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,18 @@ export interface StringifyOptions {
339339
readonly skipEmptyString?: boolean;
340340
}
341341

342+
export type Stringifiable = string | boolean | number;
343+
344+
export type StringifiableRecord = Record<
345+
string,
346+
Stringifiable | Stringifiable[] | null | undefined
347+
>;
348+
342349
/**
343350
Stringify an object into a query string and sort the keys.
344351
*/
345352
export function stringify(
346-
object: {[key: string]: any},
353+
object: StringifiableRecord,
347354
options?: StringifyOptions
348355
): string;
349356

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

360367
/**
361-
Qverrides queries in the `url` property.
368+
Overrides queries in the `url` property.
362369
*/
363-
readonly query: Record<string, string | undefined | null>;
370+
readonly query: StringifiableRecord;
364371

365372
/**
366373
Overrides the fragment identifier in the `url` property.

‎index.test-d.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,19 @@ expectType<queryString.ParsedUrl>(
9494
// Extract
9595
expectType<string>(queryString.extract('http://foo.bar/?abc=def&hij=klm'));
9696

97-
expectType<string>(queryString.stringifyUrl({url: 'https://sindresorhus.com', query: {foo: undefined}}));
97+
expectType<string>(
98+
queryString.stringifyUrl({
99+
url: 'https://sindresorhus.com',
100+
query: {
101+
fooArray: [
102+
'a',
103+
'b'
104+
],
105+
fooNumber: 1,
106+
fooBoolean: true,
107+
fooNull: null,
108+
fooUndefined: undefined,
109+
fooString: 'hi'
110+
},
111+
})
112+
);

0 commit comments

Comments
 (0)
Please sign in to comment.