diff --git a/js/url_search_params.ts b/js/url_search_params.ts index 666ba69515..8e122191d4 100644 --- a/js/url_search_params.ts +++ b/js/url_search_params.ts @@ -12,16 +12,11 @@ export class URLSearchParams { return; } - if (Array.isArray(init)) { + if (Array.isArray(init) || isIterable(init)) { this._handleArrayInitialization(init); return; } - if (isIterable(init)) { - this.params = [...init]; - return; - } - if (Object(init) !== init) { return; } @@ -285,7 +280,9 @@ export class URLSearchParams { } } - private _handleArrayInitialization(init: string[][]): void { + private _handleArrayInitialization( + init: string[][] | Iterable<[string, string]> + ): void { // Overload: sequence> for (const tuple of init) { // If pair does not contain exactly two items, then throw a TypeError. diff --git a/js/url_search_params_test.ts b/js/url_search_params_test.ts index e270dd7ff6..08b0c5a1f5 100644 --- a/js/url_search_params_test.ts +++ b/js/url_search_params_test.ts @@ -227,3 +227,12 @@ test(function urlSearchParamsCustomSymbolIterator(): void { const params1 = new URLSearchParams((params as unknown) as string[][]); assertEquals(params1.get("a"), "b"); }); + +test(function urlSearchParamsCustomSymbolIteratorWithNonStringParams(): void { + const params = {}; + params[Symbol.iterator] = function*(): IterableIterator<[number, number]> { + yield [1, 2]; + }; + const params1 = new URLSearchParams((params as unknown) as string[][]); + assertEquals(params1.get("1"), "2"); +});