microsoft / microsoft/TypeScript

Better types for UrlSearchParams

Open
#43,738 7 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: lib.d.ts Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Suggestion

🔍 Search Terms

Restrictive typing on UrlSearchParams

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

declare var URLSearchParams: {
    prototype: URLSearchParams;
    // new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams;
    new(init?: 
        | string
        | URLSearchParams
        | Record<string | number, string | number | boolean | string[] | number[] | boolean[]>
        | [string | number | boolean, string | number | boolean | string[] | number[] | boolean[]][]): URLSearchParams;
    toString(): string;
};

📃 Motivating Example

The actual typing design is more restrictive than the Javascript runtime itself.

💻 Use Cases

More possibilities to create strict typed UrlSearchParams objects:

let url = 'https://developer.microsoft.com'

const queryParams = new UrlSearchParams({
  key1: ["v1", "v2"],
  key2: ["v1", "v2", "v3"]
})

url = url + '?' + queryParams // https://developer.microsoft.com?key1=v1,v2&key2=v1,v2,v3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the standard-library declaration for URLSearchParams and review its constructor typing against the proposed overload and motivating examples. Check the surrounding type definitions and relevant type tests before deciding the supported input shapes; done means the declarations accept the intended stricter forms without changing emitted JavaScript or runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
api, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.