Typescript: allow constructing URLSearchParams directly from FormData

Created on 25 Mar 2019  路  8Comments  路  Source: microsoft/TypeScript


TypeScript Version: 3.4.0-dev.20190323


Search Terms:
URLSearchParams, FormData

Code

let form = document.createElement('form')
console.log(new URLSearchParams(new FormData(form)))

Expected behavior:
compilation succeeds and an (empty) URLSearchParams instance is constructed using the provided FormData. The above code runs without issue on Chrome 73 and Firefox 67 for me.

Actual behavior:
compilation fails because URLSearchParams doesn't explicitly allow an argument of type FormData as input to the constructor.

Argument of type 'FormData' is not assignable to parameter of type 'string | Record<string, string> | URLSearchParams | string[][]'.
  Property 'sort' is missing in type 'FormData' but required in type 'URLSearchParams'.

Playground Link:
link

Related Issues:

12517

15338

Bug lib.d.ts

Most helpful comment

Another workaround is: new URLSearchParams(new FormData(event.currentTarget) as any)

All 8 comments

If all that's required for this is to add | FormData to the constructor signature, I'm happy to PR.

19806 covers this.

@thatbudakguy - in the meantime, do you have a workaround for this?

@leonstafford my current workaround is just // @ts-ignore, sadly.

Another workaround is: new URLSearchParams(new FormData(event.currentTarget) as any)

Is there a deadline for this bug to be fixed?

No? Yes? When?

any news?

Was this page helpful?
0 / 5 - 0 ratings