Answers for "Property 'style' does not exist on type 'Element'.ts(2339)"

0

Property 'style' does not exist on type 'Element'.ts(2339)

// Cast to the type of `HTMLElement`
const el = document.getElementById('whatever') as HTMLElement;
el.style.paddingTop = ...;

// Or, if it is an array
const els = document.getElementsByClassName('my-class') as HTMLCollectionOf<HTMLElement>;
Posted by: Guest on August-09-2021
2

Property 'of' does not exist on type 'typeof Observable'.

import { of } from 'rxjs';
Posted by: Guest on October-24-2020
4

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

document.getElementById() returns the type HTMLElement which does not contain a value property.
The subtype HTMLInputElement does however contain the value property.

So a solution is to cast the result of getElementById() to HTMLInputElement like this:

var inputValue = (<HTMLInputElement>document.getElementById(elementId)).value;
<> is the casting operator in typescript.
See TypeScript: casting HTMLElement: https://fireflysemantics.medium.com/casting-htmlelement-to-htmltextareaelement-in-typescript-f047cde4b4c3

The resulting javascript from the line above looks like this:

inputValue = (document.getElementById(elementId)).value;
i.e. containing no type information.
Posted by: Guest on October-17-2020
0

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

const node = document.querySelector<HTMLElement>(element);
Posted by: Guest on August-31-2021

Code answers related to "Property 'style' does not exist on type 'Element'.ts(2339)"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language