TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)
Code
let params = new URLSearchParams({key: "730d67"})
params.toString() // "key=730d67"
Expected behavior:
The example should work.
Actual behavior:
Error: Argument of type '{ key: string }' is not assignable to parameter of type 'string | URLSearchParams`
PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes.
in the spec you linked:
[Constructor(optional (sequence<sequence<USVString>> or record<USVString, USVString> or USVString) init = ""), Exposed=(Window,Worker)]
you're forgetting the sequence<sequence<USVString>> part
i think the type should be [string, string][] | {[key: string]: string} | string
that way these would work:
new URLSearchParams([
["key", "value"]
]);
new URLSearchParams({
key: "value"
});
and these wouldn't:
// no value
new URLSearchParams([
["key"]
]);
// the value should be a string because it's converted to one
new URLSearchParams({
key: 123
});
// ???
new URLSearchParams(URLSearchParams);
i am just a fan of getting things right
This issue has not been fixed in version 2.6
Can I overrule the constructor type oversight in my own d.ts file as a quick workaround?
I now just do it brute force with any
const params = new URLSearchParams({
access_token: accessToken,
fields: friendFields.join(","),
debug: "all"
} as any);
This seems to be an issue still (or again)?
https://www.typescriptlang.org/play/#src=const%20url%20%3D%20new%20URLSearchParams(%7B%20foo%3A%201%2C%20bar%3A%202%20%7D)%0D%0A
It seems the playground is not up to date.
It understands conditional types, though. So it's at least 2.8, no?
The console says 2.9.1-insiders.20180525, but still somehow it didn't get the latest library file generated from https://github.com/Microsoft/TSJS-lib-generator.
You may test 3.0-alpha.
Having the same issue with lib.dom.d.ts typings as of 2.9.2:
root@7ca164ff6a54:/usr/app# npm list typescript
[email protected] /usr/app
`-- [email protected]
[ts]
Argument of type '{ 'filter[accountId]': number; }' is not assignable to parameter of type 'string | URLSearchParams | undefined'.
Object literal may only specify known properties, and ''filter[accountId]'' does not exist in type 'string | URLSearchParams | undefined'.
This seems to still be broken.
const params = new URLSearchParams({ "market": "USA" })
Results in a compilation failure:
(198,13): Argument of type '{ "market": string; }' is not assignable to parameter of type 'string | URLSearchParams | undefined'.
Object literal may only specify known properties, and '"market"' does not exist in type 'string | URLSearchParams | undefined'.
const params = new URLSearchParams([["market", "USA"]])
Also results in an error:
(197,44): Argument of type 'string[][]' is not assignable to parameter of type 'string | URLSearchParams | undefined'.
Type 'string[][]' is not assignable to type 'URLSearchParams'.
Property 'append' is missing in type 'string[][]'.
@NilsJPWerner What TS version are you using?
I updated my typescript and it fixed the issue!
Reminder that typescript signature does not allow using a number as a value - only string values allowed. Some people coming here may have this as the cause of the problem.
Most helpful comment
Reminder that typescript signature does not allow using a number as a value - only string values allowed. Some people coming here may have this as the cause of the problem.