-
Notifications
You must be signed in to change notification settings - Fork 424
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
fix: add FormData to URLSearchParams constructor #880
base: main
Are you sure you want to change the base?
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Add to overridingTypes.json instead of the *.generated files.
- @niklasf points out that FormData might not work when it contains a File. What happens in this case?
- Playing around, @orta and I couldn't get reasonable output from any FormData. Are we doing something wrong?
> const f = document.createElement("form")
< undefined
> const fdd = new FormData(f, { hi: "string" })
< undefined
> fdd
< FormData {append: function, delete: function, get: function, getAll: function, has: function, …}
> new URLSearchParams(fdd).toString()
> ""
@sandersn In your example the form doesn't have any form field values to map to the URLSearchParams. const form = document.createElement('FORM');
const field = document.createElement('input');
field.name = 'fieldName';
field.value = 'fieldValue';
form.appendChild(field);
const formData = new FormData(form);
const urlSearchParams = new URLSearchParams(formData);
urlSearchParams.toString(); // "fieldName=fieldValue" |
Shorter: const f = new FormData();
f.append('foo', 'bar');
new URLSearchParams(f).toString(); // foo=bar In the case of blobs it does still work, but in broken way: const f = new FormData();
f.append('foo', new Blob(['bar']));
new URLSearchParams(f).toString(); // foo=%5Bobject+File%5D |
it would be great to get this in, as this pattern is quite prominently referred to already in the third sentence in the mdn entry on |
Allowing this without a type assertion would be somewhat inconsistent. It's like changing
to
just because any |
One way could be making FormData generic. |
Any updates on this? |
I’d argue that having file inputs represented as |
@sandersn Regarding your original comment, it seems that the following comments address concerns
Are you still open to accepting this PR if |
Also happy to take this over (with guidance 😅) if needed. I think this would be a great feature to push through. I currently need it for some Next.js work. |
@ITenthusiasm I looked over the post-2020 discussion on the TS bug and I don't think this is the correct change. See my comment on microsoft/TypeScript#30584 for details. |
@sandersn Understood. Thanks for taking the time to review/reconsider the PR and the linked issue! Is there any hope of microsoft/TypeScript#43797 being seen as a viable option at some point? (Just trying to set expectations for the |
See microsoft/TypeScript#30584
I'm aware that URLSearchParams also accepts generic iterators (see #741) but mostly interested in getting this specific pattern fixed quickly, as it's used a lot.