Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Property 'style' does not exist on type 'HTMLElement'

Tags:

typescript

Although my code works (below), typescript is throwing errors saying "Property 'style' does not exist on type 'HTMLElement'."

As you can see I am grabbing the element via jQuery & the element's ID.

$(this.id).click(function () {
  this.style.display = 'none';
});

But this causes the following error:

`TS2339: Property 'style' does not exist on type 'HTMLElement'`.

I am not sure what would be the proper way of handling this ts error.

Would anyone let me know?

Thank you in advance.

more code, since it was requested:

`/**
* Class represents a flash message
*/
export class FlashMessage {    
flashMessageParentDivID:string;
iconID:string;
textID:string;
$: JQuery;

/**
 * 
 * @param {string} flashMessageParentDiv - the string of the div's ID.
 */ 
constructor (flashMessageParentDiv: string, $: JQuery) {
    this.flashMessageParentDivID = "#"+flashMessageParentDiv;
    this.iconID = "#flash-message-icon",
    this.textID = "#flash-message-text";
    this.$ = $;
}

/**
 * Displays the passed in message with the appropriate status.
 * @param {string} message - the message we want to flash.
 * @param {string} status: either 'error' or 'success'
 */
showWithMessage = function (message: string, status: string) {
    //@todo: maybe throw an error if status does not equal error or   success.
    this.clearAndCreateFlashMessageInnerds();
    this.$(this.flashMessageParentDivID).removeAttr("class");
    this.$(this.flashMessageParentDivID).addClass(status);

    this.$(this.iconID).removeAttr("class");
    this.$(this.iconID).addClass("k-icon k-i-" + status);

    this.$(this.textID).text(message);
    this.$(this.flashMessageParentDivID).show();   

    this.$(this.flashMessageParentDivID).click(function () {
        (<any>this).style.display = 'none';
    });
};

...`

typescript throwing errors

like image 795
Daedalus Avatar asked Dec 27 '17 19:12

Daedalus


2 Answers

use this in your method

(<HTMLElement>document.querySelector('#yourDomElmentID')).style.display = 'none';

test code

i test your code its compile successfully in my code.

like image 97
Manohar Gavit Avatar answered Oct 06 '22 07:10

Manohar Gavit


As Manohar Gavit suggested, I needed to typecast. But, his suggestion did not fix my problem. For some reason my jQuery definition file does not recognize HTMLElement as having the style property, but as you can see below... the definition file does accept any with that property.

`$(this.id).click(function () {
    (<any>this).style.display = 'none';
});`
like image 25
Daedalus Avatar answered Oct 06 '22 07:10

Daedalus