-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 |
By reading the You can see that
And
The spec also says that It would then make sense for Hence, the An const form = document.createElement('form')
console.log(new URLSearchParams([[new Error("foo"),"bar"]]).toString())
// Output "Error%3A+foo=bar" Which means, that |
@thatbudakguy any thoughts about the above? If you agree, would you up for re-opening this issue? |
|
I'm not convinced this needs to be reopened. When I opened it, I just wanted to quickly convert a form's data into query parameters, but had forgotten that forms can include If you're in the same situation, use the solution in #30584 (comment) with a comment about handling |
I'm not sure that this is true. There are some instances where TypeScript really should not intervene because it starts overstepping by trying to predict runtime behaviors which are intrinsically unpredictable. This is why, for example, Similarly, regarding
The comment that you linked to has negative impacts on performance by creating throw-away memory. Moreover, although I haven't tested it yet, that code might produce bugs for scenarios where a given Is there a better solution that you would propose (besides |
#43797 seems like a wholly valid solution. |
Seems limited but I'll investigate more thoroughly |
Yes, that's why TypeScript has
I doubt if TypeScript would pick
TypeScript caught mine and maybe also Budak's problem. I was writing a library that handles forms with possible File fields and confused about the error as you know URLSearchParams and FormData look so perfectly matched.
It won't, just give it a try, it works fine.
I agree that it's hard to relate with File in the first place by the error message (maybe caused by microsoft/TypeScript-DOM-lib-generator#741 that the current constructor don't allow string sequence iterables too). TypeScript should somehow improve that but it would be in different issue(s). For the slow down I used to choose
|
Those utilities do solve a number of problems. But I'm not sure I'd say they're intended for the concern that I raised earlier.
I'm basing this on a convo with orta after
That does not mean all problems can be caught (e.g., explicit
You are right. 👍🏾 This was an invalid assumption. Unfortunately, there's still the performance aspect.
It's this dancing that I don't particularly favor (when it's unnecessary). I understand that you do not frequently use the
My |
I'm not convinced this is worth re-opening. The spec snippets that @thoughtsunificator posted make it clear that Typescript should regard URLSearchParams's constructor as taking |
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: