Skip to content

Commit

Permalink
Fix the TypeScript types (sindresorhus#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdeutsch authored and sindresorhus committed Jun 20, 2019
1 parent f958329 commit cba4c93
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export interface ParseOptions {
readonly parseBooleans?: boolean;
}

export interface ParsedQuery {
readonly [key: string]: string | number | boolean | Array<string | number | boolean> | null | undefined;
export interface ParsedQuery<T = string> {
[key: string]: T | T[] | null | undefined;
}

/**
Expand All @@ -100,6 +100,9 @@ The returned object is created with [`Object.create(null)`](https://developer.mo
@param query - The query string to parse.
*/
export function parse(query: string, options: {parseBooleans: true, parseNumbers: true} & ParseOptions): ParsedQuery<string | boolean | number>;
export function parse(query: string, options: {parseBooleans: true} & ParseOptions): ParsedQuery<string | boolean>;
export function parse(query: string, options: {parseNumbers: true} & ParseOptions): ParsedQuery<string | number>;
export function parse(query: string, options?: ParseOptions): ParsedQuery;

export interface ParsedUrl {
Expand Down
7 changes: 5 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ expectType<queryString.ParsedQuery>(
expectType<queryString.ParsedQuery>(
queryString.parse('?foo=bar', {arrayFormat: 'comma'})
);
expectType<queryString.ParsedQuery>(
expectType<queryString.ParsedQuery<string | number>>(
queryString.parse('?foo=1', {parseNumbers: true})
);
expectType<queryString.ParsedQuery>(
expectType<queryString.ParsedQuery<string | boolean>>(
queryString.parse('?foo=true', {parseBooleans: true})
);
expectType<queryString.ParsedQuery<string | boolean | number>>(
queryString.parse('?foo=true', {parseBooleans: true, parseNumbers: true})
);

// Parse URL
expectType<queryString.ParsedUrl>(queryString.parseUrl('?foo=bar'));
Expand Down

0 comments on commit cba4c93

Please sign in to comment.