Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'checked' does not exist on type 'HTMLElement'.

I have this code in typescript file

 function debug_show_removed_flights() {
    if ($('.debug-window #show_removed_flights')[0].checked) {
      $('.fly-schedule-removed_reason').show();
      return $('.fly-schedule-remove').show();
    } else {
      $('.fly-schedule-removed_reason').hide();
      return $('.fly-schedule-remove').hide();
    }
  };

But in this row, I have error.

if ($('.debug-window #show_removed_flights')[0].checked) {

[ts] Property 'checked' does not exist on type 'HTMLElement'.

How I can fix it?

like image 805
Balance Avatar asked Mar 28 '18 12:03

Balance


2 Answers

Only HTMLInputElement have the checked property. You can cast your element so it will transpile:

function debug_show_removed_flights() {
    const input = $('.debug-window #show_removed_flights')[0] as HTMLInputElement;
    if (input.checked) {
        $('.fly-schedule-removed_reason').show();
        return $('.fly-schedule-remove').show();
    } else {
        $('.fly-schedule-removed_reason').hide();
        return $('.fly-schedule-remove').hide();
    }
}
like image 169
Cyril Gandon Avatar answered Sep 17 '22 07:09

Cyril Gandon


Add HTMLInputElement inline as following


function debug_show_removed_flights() {

    const input = $('.debug-window #show_removed_flights')[0];

    if ((input as HTMLInputElement).checked) {
        $('.fly-schedule-removed_reason').show();
        return $('.fly-schedule-remove').show();
    } 
    else if (!(input as HTMLInputElement).checked) {
        $('.fly-schedule-removed_reason').hide();
        return $('.fly-schedule-remove').hide();
    }
}

like image 44
Mussa Charles Avatar answered Sep 18 '22 07:09

Mussa Charles