-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
allow constructing URLSearchParams directly from FormData #30584
Comments
If all that's required for this is to add |
#19806 covers this. |
@thatbudakguy - in the meantime, do you have a workaround for this? |
@leonstafford my current workaround is just |
Another workaround is: |
Is there a deadline for this bug to be fixed? |
No? Yes? When? |
any news? |
Although Chrome, Firefox, and Safari all seem to implement support for `new URLSearchParams(formData)`, the [spec][1] doesn’t mention it, the IDL and [TypeScript][2] don’t have typings for it, and searching suggests it’s a recent addition. [1]: https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams [2]: microsoft/TypeScript#30584
I don't think it will be "fixed" as That code runs successfully in browsers by the implicit conversions in JS. Explicit matched types are needed in TS. Try to filter out the // For files, use their names
const convertedFormEntries = Array.from(
new FormData(),
([key, value]) => (
[key, typeof value === 'string' ? value : value.name]
),
);
const searchParams = new URLSearchParams(convertedFormEntries); You can see the error more clearly in this TS playground. |
Well, MDN writes that the constructor accepts one the following:
Since FormData is explicitly mentioned, TypeScript should gladly compile it and the definition file for the dom should include the mentioned types in the constructors signature. The fact that file won't show a filename but [object File] is a logical problem at runtime, not at compiletime. |
And TypeScript is all for preventing such logical typing problems. Maybe FormData could be made generic so that this can work: const data = new FormData<string>();
console.log(new URLSearchParams(data)); I'm not sure |
True, On the other hand, TypeScript should not prevent standard functionality to work as intended. |
MDN isn't the standard, strictly speaking. The file name conversion is defined in form entry list to name-value pairs conversion, HTML Standard. Click on the second bold sentence, it shows where it is used. The note about |
Now let me perform an elegant 180 after digging into it. This issue should not be labeled |
closing, since as others have mentioned it's not a bug for TypeScript to avoid coercing |
Negated types feature could clear it. in future |
Using URLSearchParams(form_data) doesn't gel well with TS. See microsoft/TypeScript#30584
Another workaround: const formData = new FormData();
formData.set('something', "something");
const searchParams = new URLSearchParams(
formData as unknown as Record<string, string>,
).toString(); if value is an object, then |
TypeScript Version: 3.4.0-dev.20190323
Search Terms:
URLSearchParams, FormData
Code
Expected behavior:
compilation succeeds and an (empty)
URLSearchParams
instance is constructed using the providedFormData
. 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 typeFormData
as input to the constructor.Playground Link:
link
Related Issues:
#12517
#15338
The text was updated successfully, but these errors were encountered: