I am getting the following error trying to build a small TypeScript project with yarn & webpack:
TS2549: Type 'URLSearchParams' is not an array type or a string type or does not have a '[Symbol.iterator]()' method that returns an iterator.
I'm trying to use a Node built-in URLSearchParams
object in a simple loop:
for (let [name, value] of searchParams) {
//do stuff
}
The Node LTS version 12 docs show that the Symbol.iterator method exists. It works in the Node REPL.
I found a closed issue in the TypeScript repo from 2017 that supposedly resolved this error. But I also tried using searchParams.entries()
and the validator did not recognize it as an available property. So it feels like my TypeScript has a very out-of-date definition for URLSearchParams. Maybe this is just a packaging issue but I don't know how to investigate further. The yarn.lock
file shows appropriate node types:
"@types/node@^12":
version "12.12.53"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.53.tgz#be0d375933c3d15ef2380dafb3b0350ea7021129"
integrity sha512-51MYTDTyCziHb70wtGNFRwB4l+5JNvdqzFSkbDvpbftEgVUBEE+T5f7pROhWMp/fxp07oNIEQZd5bbfAH22ohQ==
typescript@^3.9.7:
version "3.9.7"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.7.tgz#98d600a5ebdc38f40cb277522f12dc800e9e25fa"
integrity sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==
When I look at the local copies of the Typescript definition files I see the addition of Iterable-related properties in lib.dom.iterable.d.ts
(as mentioned in that closed GitHub issue). Somehow those are being ignored.
I can rewrite my code to use searchParams.forEach()
instead, but I'd like to understand what's going wrong anyway.
As the original post speculates, the additional definitions in lib.dom.iterable.d.ts
are not being used. The project's TypeScript config in tsconfig.json
has to reference that file:
"lib": [
"dom",
"dom.iterable",
"es2019"
]
With this, TypeScript now understands iterator-related properties for URLSearchParams and about 50 other types referenced in that file. I assume the separation is related to TypeScript backwards compatibility with older ECMAScript flavors, but I don't know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With