Sticky
Sticky

Reputation: 3959

Typescript property does not exist on type Element

I have tried to add a declaration file and a few other things but nothing is working so far:

I basically want to set visible to be a function on the HTML Element object. This is what I have and the linter has visible underlined saying Property 'visible' does not exist on type 'Element'. What am I doing wrong?

Element.prototype.visible = function() {
  return (!window.getComputedStyle(this)
    || window.getComputedStyle(this).getPropertyValue('display') == ''
    || window.getComputedStyle(this).getPropertyValue('display') != 'none')
}

I'm using Typescript 3.8

Upvotes: 0

Views: 643

Answers (1)

Aleksey L.
Aleksey L.

Reputation: 38046

You need to augment global declarations:

export {};

declare global {
    interface Element {
        visible: () => boolean;
    }
}

Element.prototype.visible = function() {
    return (!window.getComputedStyle(this)
        || window.getComputedStyle(this).getPropertyValue('display') == ''
        || window.getComputedStyle(this).getPropertyValue('display') != 'none')
};

export {}; is used to make this file a module. If you already have any other exports or imports - you don't need this

Upvotes: 1

Related Questions