Repository navigation
FormData methods should take a GenericΒ #43797
Description
Suggestion
π Search Terms
Form Data, .entries()
β Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
FormData entries() method, and perhaps .keys() and .values() should take a generic.
Right now it returns FormDataEntry value and is difficult type type the returned value of that Iterable.
π Motivating Example
Example of the problem here:
Feeding the .entries() Iterable into Object.fromEntries(), gives us a type of { [k: string]: FormDataEntryValue; }, and has no overlap between the acutal data (Person in my case) that is supposed to come back.
So perhaps something like formData.entries<Person>() or formData.entries<IterableIterator<Person>>()?
I just know it's really hard to convert the result into a type. And the solutions are either as unknown as Person or a Type Guard, which makes me manually type each property.
π» Use Cases
See above link example
Activity
max-hk commentedon May 13, 2021
How about making FormData itself generic?
So we can do something like this:
axetroy commentedon Aug 23, 2021
I am looking forward to it, it is very useful to me.
axetroy commentedon Aug 23, 2021
Here is my solution: define a custom FormData
sambitevidev commentedon Sep 30, 2021
yes that'd be great
cricketnest commentedon Oct 3, 2021
thanks, nice temp solution.
generic form data would be a great feature still.
ZinkNotTheMetal commentedon Feb 22, 2022
If anyone is using AirBnB rules and wants the above to work
RyKilleen commentedon Mar 8, 2022
Just wanted to note that this'd be a very welcome feature, especially as some newer frameworks like Remix put FormData front and center.
JohnCido commentedon Apr 18, 2022
I started using
FormDataa lot recently in a full-stack project and it's painful to use something not typed. This would be really handy.chandlervdw commentedon Apr 19, 2022
filipemir commentedon Jul 6, 2022
+1 on this suggestion. Remix's reliance on FormData is making this much more important
viktor-ulyankin commentedon Aug 3, 2022
+1
knenkne commentedon Aug 5, 2022
+1
DigitalNaut commentedon Aug 22, 2022
+1
7 remaining items
alojzy231 commentedon Oct 17, 2022
+1
mohamedanwer123 commentedon Oct 18, 2022
+1
cdeutsch commentedon Oct 18, 2022
Please stop with the +1's. Everyone subscribed to this issue gets a notification for it.
Use the emoji reactions on the first post instead.
DrakkoFire commentedon Nov 11, 2022
Any news on this ?
elving commentedon Nov 11, 2022
This is what I ended up doing:
DrakkoFire commentedon Nov 11, 2022
Pretty convenient, thanks Elving RodrΓguez (@elving) :)
karlhorky commentedon Feb 15, 2023
I asked for a similar feature for
ResponseandRequestinterfaces to accept generic type parameters: #52777karlhorky commentedon Feb 20, 2023
Additional use case
A typed API client function would be a use case for this:
TypeScript Playground
twicer-is-coder commentedon Sep 24, 2023
+1
adophilus commentedon Oct 14, 2023
+1
verber33 commentedon Nov 14, 2023
+1
zonzujiro commentedon Feb 9, 2024
+1
ayech0x2 commentedon Jun 3, 2024
+1
k1eu commentedon Aug 12, 2024
You can check out my library for just doing this thing :) Its built on the FormData primitive so can be used in replacement of native FormData :)
https://github.com/k1eu/typed-formdata
notpushkin commentedon Jan 17, 2025
Tomasz Kielar (@k1eu) Thank you so much, this is incredible!
If anybody wants a version of this without any runtime code, itβs possible to make
TypedFormDataa generic interface. Hereβs a simple example:(Iβve also removed the
| nullclause here, so that you can denote optional fields)