Typescript: What's the correct way to get URLSearchParams into my project?

Created on 27 Nov 2016  路  9Comments  路  Source: microsoft/TypeScript

I have some code where I'd like to be able to build query strings. Unfortunately I can't seem to find a polyfill similar to what I'm currently doing with fetch.

Is there any correct way to get these newer standards into my typescript projects, along with any actual polyfill code to accompany my build?

Bug lib.d.ts Fixed

Most helpful comment

URLSearchParams is still an experimental feature. we do not add DOM libraries to the standard library at this stage to avoid breaking users in the future. that said, interfaces are interfaces, and do not need to be in lib.d.ts specifically. you would need to declare it:

declare class URLSearchParams {
    /** Constructor returning a URLSearchParams object. */
    constructor(init?: string| URLSearchParams);

    /**Appends a specified key/value pair as a new search parameter. */
    append(name: string, value: string): void

    /** Deletes the given search parameter, and its associated value, from the list of all search parameters. */
    delete(name: string): void; 

    /** Returns an iterator allowing to go through all key/value pairs contained in this object. */
    entries(): IterableIterator<[string, string]>;

    /** Returns the first value associated to the given search parameter. */
    get(name: string): string;

    /** Returns all the values association with a given search parameter. */
    getAll(name: string): string[];

    /** Returns a Boolean indicating if such a search parameter exists. */
    has(name: string): boolean;

    /** Returns an iterator allowing to go through all keys of the key/value pairs contained in this object. */
    keys(): IterableIterator<string>;

    /** Sets the value associated to a given search parameter to the given value. If there were several values, delete the others. */
    set(name: string, value: string): void;

    /** Returns a string containg a query string suitable for use in a URL. */
    toString(): string;

    /** Returns an iterator allowing to go through all values of the key/ value pairs contained in this object. */
    values(): IterableIterator<string>;

     /** Iterator */
    [Symbol.iterator](): IterableIterator<number>;
}

All 9 comments

As indicated in the template, this is not a support forum. Please ask questions on Stack Overflow or similar sites.

This is not a question or support request.

I'm looking for an implementation of URLSearchParams to use in my TypeScript projects. Similar to how you offer other libraries via the --lib flag.

I don't understand what about that isn't a question. Are you saying there's a bug in TypeScript?

URLSearchParams is still an experimental feature. we do not add DOM libraries to the standard library at this stage to avoid breaking users in the future. that said, interfaces are interfaces, and do not need to be in lib.d.ts specifically. you would need to declare it:

declare class URLSearchParams {
    /** Constructor returning a URLSearchParams object. */
    constructor(init?: string| URLSearchParams);

    /**Appends a specified key/value pair as a new search parameter. */
    append(name: string, value: string): void

    /** Deletes the given search parameter, and its associated value, from the list of all search parameters. */
    delete(name: string): void; 

    /** Returns an iterator allowing to go through all key/value pairs contained in this object. */
    entries(): IterableIterator<[string, string]>;

    /** Returns the first value associated to the given search parameter. */
    get(name: string): string;

    /** Returns all the values association with a given search parameter. */
    getAll(name: string): string[];

    /** Returns a Boolean indicating if such a search parameter exists. */
    has(name: string): boolean;

    /** Returns an iterator allowing to go through all keys of the key/value pairs contained in this object. */
    keys(): IterableIterator<string>;

    /** Sets the value associated to a given search parameter to the given value. If there were several values, delete the others. */
    set(name: string, value: string): void;

    /** Returns a string containg a query string suitable for use in a URL. */
    toString(): string;

    /** Returns an iterator allowing to go through all values of the key/ value pairs contained in this object. */
    values(): IterableIterator<string>;

     /** Iterator */
    [Symbol.iterator](): IterableIterator<number>;
}

@mhegazy This doesn't seem to be fixed in 2.1.4. I see it has the 2.2 milestone now. Is it available in nightly?

The fix is checked in the lib repo and needs to be ported in. We do them in patches; once it is in the nightly we will close the issue.

Thanks for the explanation.

Yay!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Antony-Jones picture Antony-Jones  路  3Comments

uber5001 picture uber5001  路  3Comments

wmaurer picture wmaurer  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

seanzer picture seanzer  路  3Comments