React-Native-Tips icon indicating copy to clipboard operation
React-Native-Tips copied to clipboard

Use same format in Typescript

Open josauder opened this issue 6 years ago • 16 comments

Hi, I was wondering how to to the same thing in typescript. I get an error with this line:

data.append('photo', {
  uri: photo.uri,
  type: 'image/jpeg', 
  name: 'testPhotoName'
});

Because of a type mismatch:

Argument of type '{ uri: string; name: string; type: string; }' is not assignable to parameter of type 'string | Blob'.
  Object literal may only specify known properties, and 'uri' does not exist in type 'string | Blob'.

The documentation of FormData.append looks nothing like the code described above. Does anybody have an idea how to solve this?

josauder avatar Dec 18 '17 16:12 josauder

I have the same issue with flowtype. It shows me three possible methods that do not fit my needs:

  • append(name: string, value: string): void;
  • append(name: string, value: Blob, filename?: string): void;
  • append(name: string, value: File, filename?: string): void;

challme28 avatar Feb 19 '18 02:02 challme28

For the blob parameter use: JSON.stringify({ uri: photo.uri, type: 'image/jpeg', name: 'testPhotoName' });

LorandP avatar Jun 01 '18 13:06 LorandP

JSON.stringify didn't work for me. @josauder @challme28 Were you guys able to get this to work?

bpred754 avatar Aug 31 '19 21:08 bpred754

For anyone else that comes across this. @LorandP's answer didn't work, but it was close. I was getting JSON parse [EOF] errors. This is what I ended up with:

JSON.parse(JSON.stringify({ uri: photo.uri, type: 'image/jpeg', name: 'testPhotoName' }));

Probably not the best approach, but I spent far too long trying to find a better way.

bpred754 avatar Aug 31 '19 22:08 bpred754

Same issue, how to fix this?

cuongndc avatar Dec 03 '19 10:12 cuongndc

@josauder did you solve the problem? How?

dickerpulli avatar Feb 26 '20 23:02 dickerpulli

JSON.parse(JSON.stringify({ uri: photo.uri, type: 'image/jpeg', name: 'testPhotoName' }));

Isn't this basically just the same as type casting as any?

ahmed-ismail-nt avatar Aug 08 '21 00:08 ahmed-ismail-nt

JSON.parse(JSON.stringify({ uri: photo.uri, type: 'image/jpeg', name: 'testPhotoName' }));

Isn't this basically just the same as type casting as any?

exactly

albertoammar avatar Dec 26 '21 12:12 albertoammar

It's better to cast to unknown and then to Blob.

{} as unknown as Blob

witalobenicio avatar Jun 24 '22 21:06 witalobenicio

Same problem here... I got this error and I was unable to fix any other solution other than casting?

LeandrodeLimaC avatar Nov 13 '22 02:11 LeandrodeLimaC

@witalobenicio same here,

 const imgData = new FormData();
 const imgData = new FormData();
    imgData.append("files", {
      uri: image,
      name: "image.jpg",
      type: "image/jpg",
    } as unknown as Blob);
    console.log("i am imgData", imgData);

the output is empty ( i am imgData FormData {}), the formData is empty

lavin67 avatar Nov 16 '22 14:11 lavin67