TS2339: Property 'style' does not exist on type 'Element'

JavascriptTypescript

Javascript Problem Overview


Here's the code:

const test = Array.from(document.getElementsByClassName('mat-form-field-infix'));
test.forEach((element) => {
    element.outerHTML = '<div class="good-day-today" style="width: 0px;"></div>'; // Please note that this line works fine!
    element.style.padding = '10px';
    element.style.borderTop = '0';
});

Error I get when compiled:

> ERROR in src/app//.component.ts(101,21): error TS2339: Property > 'style' does not exist on type 'Element'. > src/app//.component.ts(102,21): error TS2339: Property 'style' > does not exist on type 'Element'.

How can I fix it?

I tried to remove the Array.from... part, tried to use for of and for in, tried as any, but above is the way I have to do it.

Javascript Solutions


Solution 1 - Javascript

You need a typecast:

Array.from(document.getElementsByClassName('mat-form-field-infix') as HTMLCollectionOf<HTMLElement>)

That's because getElementsByClassName only returns HTMLCollection<Element>, and Element does not have a styleproperty. The HTMLElement however does implement it via it's ElementCSSInlineStyle extended interface.

Note that this typecast is typesafe in the way that every Elementis either a HTMLElement or an SVGElement, and I hope that your SVG Elements don't have a class.

Solution 2 - Javascript

Another option is to use querySelectorAll and a type parameter. getElementsByClassName is not generic, but querySelectorAll is - you can just pass the type of the elements that will be selected, like this:

const test = document.querySelectorAll<HTMLElement>('.mat-form-field-infix');

This doesn't require any type casting, and it will allow you to use forEach immediately, rather than converting it to an array first. (getElementsByClassName returns an HTMLCollection, which does not have a forEach method; querySelectorAll returns a NodeList, which does have a forEach method, at least on somewhat up-to-date browsers. To support ancient browsers too, you'll need a polyfill, or convert it to an array first.)

If you happen to just need a single element, you can use querySelector, which is generic as well:

const elm = document.querySelector<HTMLElement>('.foo')!;
elm.style.padding = '10px';

Another benefit of querySelectorAll (and querySelector) over the many other options is that they accept CSS selector strings, which can be far more flexible and precise. For example, the selector string

.container > input:checked

will select children of <div class="container"> which are <input>s and are checked.

Solution 3 - Javascript

When you set the outerHTML, you're destroying the original element that was there. So, your styling doesn't work.

You'll notice that if you change it to set innerHTML, your styling does work.

This does not do the same exact thing, but I hope it points you in the right direction.

const test = Array.from(document.getElementsByClassName('mat-form-field-infix'));
test.forEach((element) => {
    element.innerHTML = '<div class="good-day-today" style="width: 0px;"></div>'; // Please note that this line works fine!
    element.style.padding = '10px';
    element.style.borderTop = '0';
});

Solution 4 - Javascript

I was also facing a similar type of issue while doing

document.querySelectorAll(".<className>"); 

so instead of adding the style property, I simply got around it by just adding another class.

example:

//css file
    .<classname> {
        display: none;
      }
    .<classname>.show {
        display: flex;
      }

//ts file
elements.forEach((ele, index) => {
const errors = something.length;
if (index < errors) {
  ele.classList.add("show");
} else {
  ele.classList.remove("show");
}

}); };

Solution 5 - Javascript

A workaround could be doing something like this:

element["style"].padding = '10px';
element["style"].borderTop = '0';

Maybe it's not the best solution, but it should work, I used it multiple times :)

Solution 6 - Javascript

I think I found a way easier method:

Just create an index.d.ts file and add:

interface Element {
	style: CSSStyleDeclaration
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmazingDayTodayView Question on Stackoverflow
Solution 1 - JavascriptJonas WilmsView Answer on Stackoverflow
Solution 2 - JavascriptCertainPerformanceView Answer on Stackoverflow
Solution 3 - JavascriptAndrewView Answer on Stackoverflow
Solution 4 - JavascriptrasujanView Answer on Stackoverflow
Solution 5 - JavascriptCarlo StanzioneView Answer on Stackoverflow
Solution 6 - JavascriptLevminerView Answer on Stackoverflow