Node: tighten URL query type (#33997)

* Add failing test

* Fix failing test

* Specify min version

* Name type and export

* Move type

* Use interface

* Use index signature

* Relocate test, fix 2.0 support

* Replace type alias with inline type + comments
This commit is contained in:
Oliver Joseph Ash
2019-04-08 12:41:52 -07:00
committed by Nathan Shively-Sanders
parent e1be1d24b7
commit d9f74cb42c
3 changed files with 17 additions and 4 deletions
+8 -1
View File
@@ -10,7 +10,14 @@ declare module "querystring" {
interface ParsedUrlQuery { [key: string]: string | string[]; }
function stringify(obj?: {}, sep?: string, eq?: string, options?: StringifyOptions): string;
interface ParsedUrlQueryInput {
[key: string]:
// The value type here is a "poor man's `unknown`". When these types support TypeScript
// 3.0+, we can replace this with `unknown`.
{} | null | undefined;
}
function stringify(obj?: ParsedUrlQueryInput, sep?: string, eq?: string, options?: StringifyOptions): string;
function parse(str: string, sep?: string, eq?: string, options?: ParseOptions): ParsedUrlQuery;
function escape(str: string): string;
function unescape(str: string): string;
+7 -1
View File
@@ -1,6 +1,6 @@
import * as querystring from 'querystring';
interface SampleObject { a: string; }
interface SampleObject { [key: string]: string; }
{
const obj: SampleObject = { a: "" };
@@ -40,3 +40,9 @@ interface SampleObject { a: string; }
result = querystring.escape(str);
result = querystring.unescape(str);
}
{
const queryInput: string | null | querystring.ParsedUrlQueryInput = {};
// $ExpectError
const query: string | null | querystring.ParsedUrlQuery = queryInput;
}
+2 -2
View File
@@ -1,5 +1,5 @@
declare module "url" {
import { ParsedUrlQuery } from 'querystring';
import { ParsedUrlQuery, ParsedUrlQueryInput } from 'querystring';
interface UrlObjectCommon {
auth?: string;
@@ -17,7 +17,7 @@ declare module "url" {
// Input to `url.format`
interface UrlObject extends UrlObjectCommon {
port?: string | number;
query?: string | null | { [key: string]: any };
query?: string | null | ParsedUrlQueryInput;
}
// Output of `url.parse`