In this article we will learn about some of the frequently asked TypeScript programming questions in technical like “Property ‘style’ does not exist on type ‘Element’.ts(2339)” Code Answer’s. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error or stack handling on typescript was simple and easy. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in Typescript. Below are some solution about “Property ‘style’ does not exist on type ‘Element’.ts(2339)” Code Answer’s.
Property ‘of’ does not exist on type ‘typeof Observable’.
xxxxxxxxxx
1
import { of } from 'rxjs';
2
Property ‘value’ does not exist on type ‘HTMLElement’.
xxxxxxxxxx
1
document.getElementById() returns the type HTMLElement which does not contain a value property.
2
The subtype HTMLInputElement does however contain the value property.
3
4
So a solution is to cast the result of getElementById() to HTMLInputElement like this:
5
6
var inputValue = (<HTMLInputElement>document.getElementById(elementId)).value;
7
<> is the casting operator in typescript.
8
See TypeScript: casting HTMLElement: https://fireflysemantics.medium.com/casting-htmlelement-to-htmltextareaelement-in-typescript-f047cde4b4c3
9
10
The resulting javascript from the line above looks like this:
11
12
inputValue = (document.getElementById(elementId)).value;
13
i.e. containing no type information.
Property ‘style’ does not exist on type ‘Element’.ts(2339)
xxxxxxxxxx
1
// Cast to the type of `HTMLElement`
2
const el = document.getElementById('whatever') as HTMLElement;
3
el.style.paddingTop = ;
4
5
// Or, if it is an array
6
const els = document.getElementsByClassName('my-class') as HTMLCollectionOf<HTMLElement>;
TS2339: Property ‘style’ does not exist on type ‘Element’
xxxxxxxxxx
1
TS2339: Property ‘style’ does not exist on type ‘Element’