0

I am trying to convert this code into TypeScript:

  methods: {
    handleImage(e) {
      const selectedImage = e.target.files[0];
      this.createVase64Image(selectedImage);
    },
    createBase64Image(fileObject) {
      const reader = new FileReader();

      reader.onload = (e) => {
        this.image = e.target.result;
      };
      reader.readAsBinaryString(fileObject);
    }
  }

I went and copy-pasted the method and I am receiving this error from the compiler:

Parameter 'e' implicitly has an 'any' type.

What is the meaning of the "e", what is it used for and how do I convert the line of code above into TypeScript?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Daniel
  • 109
  • 1
  • 13
  • 3
    Well, what _are_ you expecting to get passed to `handleImage`? An [event](https://developer.mozilla.org/en-US/docs/Web/Events)? To convert JS to TS you generally have to add _type information_, and random people on the internet (hi!) can't necessarily tell you what types are in use in the codebase _you're working on_. – jonrsharpe Jun 24 '21 at 07:58
  • 1
    TypeScript is a superset of JavaScript. All valid JavaScript is valid TypeScript. You see this error because of the configuration in your `tsconfig.json`. The best way to solve the problem is to use the correct type. Much worse solutions are to disable this check or to use type `any` (but you really shouldn't do that). –  Jun 24 '21 at 08:04
  • If you just want the warning to go away, change `(e)` to `(e: any)`. It's not an error, it's just warning you that the type isn't explicitly defined. – Amunium Jun 24 '21 at 08:06
  • @Amunium Usually it's an error and it breaks the build process. –  Jun 24 '21 at 08:06
  • The main question is why do you use TypeScript without types? The main reason for TypeScript is the strict typing. It doesn't make sense to use TypeScript without types. Name your files with `*.js` and it should solve the problem. –  Jun 24 '21 at 08:10
  • If you don't want this error and don't want to type all things, in your tsconfig.json, in `compilerOptions` you can add the key `"noImplicitAny": false` ([see here](https://www.typescriptlang.org/tsconfig#noImplicitAny)) – Ken La Jun 24 '21 at 08:10
  • @jonrsharpe I am trying to pass an image to `handleImage`. After adding the type for e as a File `handleImage(e: File) { const selectedImage = e.target.files[0]; this.createBase64Image(selectedImage); }` I'm getting an error of `Property 'target' does not exist on type 'File'.` Can u clarify me on what type should I use when passing an image/file. – Daniel Jun 24 '21 at 08:12
  • 1
    Well the code alone tells you it needs to be something that has a `target` property, which `File` does not. – jonrsharpe Jun 24 '21 at 08:13
  • Try `e: Event`. `e` is a common parameter name for an event and events usually have targets. –  Jun 24 '21 at 08:15
  • 1
    https://stackoverflow.com/questions/43176560/property-files-does-not-exist-on-type-eventtarget-error-in-typescript – Roberto Zvjerković Jun 24 '21 at 09:03
  • @RobertoZvjerković Thank you very much. After reading this I was able to read the file on the `handleImage` function. A new problem error occurs whereby `"InvalidStateError: Failed to execute 'readAsBinaryString' on 'FileReader': The object is already busy reading Blobs."` – Daniel Jun 24 '21 at 14:26

0 Answers0