[TypeScript] Can't be used as fetch body
See original GitHub issueMDN example of fetch()
with FormData
:
const formData = new FormData();
const fileField = document.querySelector('input[type="file"]');
formData.append('username', 'abc123');
formData.append('avatar', fileField.files[0]);
fetch('https://example.com/profile/avatar', {
method: 'PUT',
body: formData
})
.then(response => response.json())
.then(result => {
console.log('Success:', result);
})
.catch(error => {
console.error('Error:', error);
});
I imported form-data
so that I could test it with jest
:
import FormData from 'form-data'
const formData = new FormData();
formData.append('name', 'abc123');
fetch('https://example.com/profile/avatar', {
method: 'PUT',
body: formData
})
.then(response => response.json())
.then(result => {
console.log('Success:', result);
})
.catch(error => {
console.error('Error:', error);
});
Now it fails with:
Type 'FormData' is not assignable to type 'BodyInit | null | undefined'.
Type 'FormData' is missing the following properties from type 'URLSearchParams': delete, get, getAll, has, and 3 more.ts(2322)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:7
- Comments:8
Top Results From Across the Web
Using fetch with TypeScript - Kent C. Dodds
In EpicReact.dev workshops, when I'm teaching how to make HTTP requests, I use the GraphQL Pokemon API. Here's how we make that request:...
Read more >How to Use fetch with TypeScript -- newline - Fullstack.io
The main problem with fetch function is that it isn't a generic function. This makes it harder to type response data without writing...
Read more >promise - How to use fetch in TypeScript - Stack Overflow
fetch in Typescript, but I cannot cast the response directly to my custom type: I am hacking my way around this by casting...
Read more >TypeError: cannot use 'in' operator to search for 'x' in 'y'
Make sure the object you are inspecting isn't actually null or undefined . const foo = null; "bar" ...
Read more >The starting point for learning TypeScript
Get Started. Quick introductions based on your background ... JavaScript. How to use TypeScript-powered JavaScript tooling. JS Projects Utilizing TypeScript ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m getting the same typescript error too, I opted not to use this library and just use URLSearchParams.
Since they do the same thing.
@aprilmintacpineda No they don’t do the same thing. URLSearchParams will encode it as
application/x-www-form-urlencoded
where this library will encode it asmultipart/form-data
. You can’t upload files withapplication/x-www-form-urlencoded
but withmultipart/form-data
you can. If you want to upload binary data, usemultipart/form-data
.