Skip to content

URLSearchParams constructor argument should be string | object #15338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
saschanaz opened this issue Apr 24, 2017 · 14 comments · Fixed by microsoft/TypeScript-DOM-lib-generator#450
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this

Comments

@saschanaz
Copy link
Contributor

TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)

Code

By the spec:

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`

@saschanaz saschanaz changed the title URLSearchParams constructor argument should be any URLSearchParams constructor argument should be object Apr 24, 2017
@saschanaz saschanaz changed the title URLSearchParams constructor argument should be object URLSearchParams constructor argument should be string | object Apr 24, 2017
@mhegazy mhegazy added Help Wanted You can do this Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Apr 24, 2017
@mhegazy mhegazy added this to the Community milestone Apr 24, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Apr 24, 2017

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.

@ghost
Copy link

ghost commented Apr 25, 2017

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

jacobrask added a commit to jacobrask/TypeScript that referenced this issue Jun 19, 2017
See https://url.spec.whatwg.org/#interface-urlsearchparams
Added sequence<sequence<USVString>> and record<USVString, USVString>.

Fixes microsoft#15338
jacobrask added a commit to jacobrask/TypeScript that referenced this issue Jun 19, 2017
See https://url.spec.whatwg.org/#interface-urlsearchparams
Added sequence<sequence<USVString>> and record<USVString, USVString>.

Fixes microsoft#15338
jacobrask added a commit to jacobrask/TSJS-lib-generator that referenced this issue Jun 19, 2017
@kgtkr
Copy link

kgtkr commented Nov 1, 2017

This issue has not been fixed in version 2.6

@0x80
Copy link

0x80 commented Nov 28, 2017

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);

@lazarljubenovic
Copy link

@saschanaz
Copy link
Contributor Author

It seems the playground is not up to date.

@lazarljubenovic
Copy link

It understands conditional types, though. So it's at least 2.8, no?

@saschanaz
Copy link
Contributor Author

saschanaz commented Jul 18, 2018

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.

@tarekis
Copy link

tarekis commented Sep 27, 2018

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'.

@NilsJPWerner
Copy link

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[][]'.

@saschanaz
Copy link
Contributor Author

@NilsJPWerner What TS version are you using?

@NilsJPWerner
Copy link

I updated my typescript and it fixed the issue!

@klesun
Copy link

klesun commented Mar 11, 2020

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.

@tlmader
Copy link

tlmader commented Jul 25, 2023

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.

Exactly why I'm here 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants