FormData methods should take a Generic
See original GitHub issueSuggestion
🔍 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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:74
- Comments:26
Top GitHub Comments
How about making FormData itself generic?
So we can do something like this:
I started using
FormData
a lot recently in a full-stack project and it’s painful to use something not typed. This would be really handy.