Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'File' object does not have a 'path' property. (TypeScript)

I am passing a File object that comes from the browser (by dropping) to a function like this:

// .... logic here...
accept(file)

public static accept(file: File) {
   console.log(file)
   /* prints:
      => lastModified: 1555453309243
         lastModifiedDate: Wed Apr 17 2019 01:21:49 GMT+0100 (GMT+01:00) {}
         name: "test.txt"
         path: "test.txt" --> as you can see there is a path variable in here.
         size: 16
         type: "text/plain"
         webkitRelativePath: ""
         __proto__: File
   */

   console.log(file.name) // => prints 'test.txt'
   console.log(file.size) // => prints '16'
   console.log(file.path) // => !! error given. path does not exists in File object. Also IDE does not show this parameter in IDE autocomplete list.
}

Error given:

Property 'path' does not exist on type 'File'.ts(2339)

Why File does not have path parameter? How can I make sure path exists? Is there any other type for File? This is a HTML5 drop.

If I do this:

public static accept(file: any) {
   alert(file.path);
}

It shows the relative path:

path

like image 968
Dennis Avatar asked Nov 16 '22 11:11

Dennis


1 Answers

For anyone else using react-dropzone, it seems they're getting the typing for FileWithPath from this library file-selector

See line 3 here: https://github.com/react-dropzone/react-dropzone/blob/master/typings/react-dropzone.d.ts

You can simply import this FileWithPath from react-dropzone wherever you need it.

like image 163
Kitson Avatar answered Jan 03 '23 17:01

Kitson