Typescript: Property 'src' does not exist on type 'HTMLElement'

Björn Hjorth

I get a error from Typescript and I am not sure how to correct it. The code works fine when "compiled" but I can't correct the error. I have extracted the parts that involve the error from my code. I guess I have to predifine the src but not sure how.

Error msg in Editor and on Gulp compile:

"Property 'src' does not exist on type 'HTMLElement'.at line 53 col 17"

...

element:HTMLElement; /* Defining element */

'''
this.element = document.createElement('img'); /*creating a img*/

'''

This is the method I run to render the element, position, top and left all works with out giving a error.

display() {
   this.element.src = this.file; /*This is the line that gives the error*/
   this.element.style.position = "absolute";
   this.element.style.top = this.pointX.toString() + "px";
   this.element.style.left = this.pointY.toString() + "px";

   document.body.appendChild(this.element);
};
John Weisz

Because src is not a property of the HTMLElement type, but of HTMLImageElement.

If you are certain you'll get an img element, you might want to declare your variable with the correct subtype:

element: HTMLImageElement; /* Defining element */

// ...

this.element = document.createElement('img'); /*creating a img*/

Also, you might want to have a look at what document.createElement returns. It's the very same type if you specify "img" as its argument.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Typescript - Property 'scrollY' does not exist on type 'HTMLElement'

From Java

useRef Typescript error: Property 'current' does not exist on type 'HTMLElement'

From Dev

Typescript error Property does not exist on type 'HTMLElement'. any

From Dev

Property 'noUiSlider' does not exist on type 'HTMLElement'

From Java

The property 'value' does not exist on value of type 'HTMLElement'

From Dev

Property 'noUiSlider' does not exist on type 'HTMLElement'

From Java

Property 'value' does not exist on type EventTarget in TypeScript

From Java

jest typescript property mock does not exist on type

From Dev

Typescript property does not exist on type 'ElementFinder'

From Dev

TypeScript - Property 'id' does not exist on type 'Node'

From Dev

TypeScript: Property 'propertyName' does not exist on type 'Function'

From Dev

Property 'load' does not exist on type TypeScript?

From Dev

Typescript property does not exist on type 'ElementFinder'

From Dev

class declaration - Property does not exist on type in TypeScript

From Dev

iframe inside angular2 component, Property 'contentWindow' does not exist on type 'HTMLElement'

From Dev

Typescript throws a property does not exist on type even with type file included

From Dev

property selectedIndex does not exists on type HTMLElement

From Java

Ignore Typescript Errors "property does not exist on value of type"

From Dev

Typescript compile error: Property 'classList' does not exist on type 'Node'

From Dev

How to avoid typescript error: Property 'innerHTML' does not exist on type 'Element'

From Dev

Property 'name' does not exist on type 'JSON' in typescript / angularJs 2

From Java

Typescript + React/Redux: Property "XXX" does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes

From Dev

Typescript error: Property 'value' does not exist on type 'Observable<any>'

From Java

How to prevent "Property '...' does not exist on type 'Global'" with jsdom and typescript?

From Dev

How to remove TypeScript warning: property 'length' does not exist on type '{}'

From Java

Typescript tells me property 'padStart' does not exist on type 'string'. Why?

From Dev

Typescript Error TS2339: Property 'project' does not exist on type '{}'

From Dev

RxJs / Typescript throws 'property clientX does not exist on type {}'

From Dev

Typescript compile error: Property 'bodyParser' does not exist on type 'typeof e'

Related Related

  1. 1

    Typescript - Property 'scrollY' does not exist on type 'HTMLElement'

  2. 2

    useRef Typescript error: Property 'current' does not exist on type 'HTMLElement'

  3. 3

    Typescript error Property does not exist on type 'HTMLElement'. any

  4. 4

    Property 'noUiSlider' does not exist on type 'HTMLElement'

  5. 5

    The property 'value' does not exist on value of type 'HTMLElement'

  6. 6

    Property 'noUiSlider' does not exist on type 'HTMLElement'

  7. 7

    Property 'value' does not exist on type EventTarget in TypeScript

  8. 8

    jest typescript property mock does not exist on type

  9. 9

    Typescript property does not exist on type 'ElementFinder'

  10. 10

    TypeScript - Property 'id' does not exist on type 'Node'

  11. 11

    TypeScript: Property 'propertyName' does not exist on type 'Function'

  12. 12

    Property 'load' does not exist on type TypeScript?

  13. 13

    Typescript property does not exist on type 'ElementFinder'

  14. 14

    class declaration - Property does not exist on type in TypeScript

  15. 15

    iframe inside angular2 component, Property 'contentWindow' does not exist on type 'HTMLElement'

  16. 16

    Typescript throws a property does not exist on type even with type file included

  17. 17

    property selectedIndex does not exists on type HTMLElement

  18. 18

    Ignore Typescript Errors "property does not exist on value of type"

  19. 19

    Typescript compile error: Property 'classList' does not exist on type 'Node'

  20. 20

    How to avoid typescript error: Property 'innerHTML' does not exist on type 'Element'

  21. 21

    Property 'name' does not exist on type 'JSON' in typescript / angularJs 2

  22. 22

    Typescript + React/Redux: Property "XXX" does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes

  23. 23

    Typescript error: Property 'value' does not exist on type 'Observable<any>'

  24. 24

    How to prevent "Property '...' does not exist on type 'Global'" with jsdom and typescript?

  25. 25

    How to remove TypeScript warning: property 'length' does not exist on type '{}'

  26. 26

    Typescript tells me property 'padStart' does not exist on type 'string'. Why?

  27. 27

    Typescript Error TS2339: Property 'project' does not exist on type '{}'

  28. 28

    RxJs / Typescript throws 'property clientX does not exist on type {}'

  29. 29

    Typescript compile error: Property 'bodyParser' does not exist on type 'typeof e'

HotTag

Archive