Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Error: Property 'files' does not exist on type 'HTMLElement'

I wish to create an upload function for my Apps by using IONIC.

Here is my HTML code:

<input ion-button block type="file" id="uploadBR">
<input ion-button block type="file" id="uploadIC">
<button ion-button block (click)="upload()">Confirm Claim Restaurant</button>

Here is my upload() function:

upload(){   
   let BR = document.getElementById('uploadBR').files[0]
   let IC = document.getElementById('uploadIC').files[0]
   console.log(BR)
   console.log(IC)    
 }

In normal HTML it should work, but it doesn't work with my IONIC.

When building the App, it will show the error Typescript Error: Property 'files' does not exist on type 'HTMLElement'.

Am i do it in wrong way or it has to be done in different way with typescript?

Thanks.

like image 680
Jerry Avatar asked Apr 14 '18 02:04

Jerry


People also ask

Does not exist on type HTMLElement TS?

The error "Property 'X' does not exist on type 'HTMLElement'" occurs when we try to access a property that doesn't exist on an element of type HTMLElement . To solve the error, use a type assertion to type the element correctly before accessing the property. This is the index.

What is HTMLInputElement?

The HTMLInputElement interface provides special properties and methods for manipulating the options, layout, and presentation of <input> elements.


1 Answers

You neet to cast it as a HTMLInputElement as files is a property of input element

let BR = (<HTMLInputElement>document.getElementById('uploadBR')).files[0];
like image 189
ranjeet8082 Avatar answered Sep 18 '22 11:09

ranjeet8082