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

Bjarke Freund-Hansen

I am playing around with typescript and am trying to create a script that will update a p-element as text is inputted in a input box.

The html looks as following:

<html>
    <head>
    </head>
    <body>
        <p id="greet"></p>
        <form>
            <input id="name" type="text" name="name" value="" onkeyup="greet('name')" />
        </form>
    </body>
    <script src="greeter.js"></script>
</html>

And the greeter.ts file:

function greeter(person)
{
    return "Hello, " + person;
}

function greet(elementId)
{
    var inputValue = document.getElementById(elementId).value;

    if (inputValue.trim() == "")
        inputValue = "World";

    document.getElementById("greet").innerText = greeter(inputValue);
}

When I compile with tsc I get the following "error":

/home/bjarkef/sandbox/greeter.ts(8,53): The property 'value' does not exist on value of type 'HTMLElement'

However the compiler does output a javascript file, which works just fine in chrome.

How come I get this error? And how can I fix it?

Also, where can I look up which properties are valid on a 'HTMLElement' according to typescript?

Please note I am very new to javascript and typescript, so I might be missing something obvious. :)

Bjarke Freund-Hansen

Based on Tomasz Nurkiewiczs answer, the "problem" is that typescript is typesafe. :) So the 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 the question TypeScript: casting HTMLElement.

The resulting javascript from the line above looks like this:

inputValue = (document.getElementById(elementId)).value;

i.e. containing no type information.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Java

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

From Java

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

From Dev

The property 'jqGrid' does not exist on value of type 'JQuery'

From Dev

Property 'value' does not exist on type 'Element' angular

From Dev

JSDoc: Property 'value' does not exist on type 'EventTarget'

From Dev

Property 'value' does not exist on type Element

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 Java

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

From Dev

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

From Dev

The property 'xyz' does not exist on value of [imported] type 'xyz' in Typescript

From Dev

HTML Element - Does "value" attribute or property exist?

From Dev

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

From Dev

property selectedIndex does not exists on type HTMLElement

From Java

The default value type does not match the type of the property

From Dev

Uncaught TypeError: Cannot read property 'value' of null, Element does exist

From Dev

Uncaught TypeError: Cannot read property 'value' of null, Element does exist

From Dev

How to add default value if given property does not exist in lodash template

From Dev

Property does not exist on a function's return value of multiple types

From Dev

Property 'count' does not exist on type '{}'

From Dev

Property 'then' does not exist on type 'void'

From Java

'Property does not exist on type 'never'

From Dev

Property 'json' does not exist on type '{}'

From Dev

Property push does not exist on type

From Dev

Angular - Property does not exist on type

Related Related

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

    The property 'jqGrid' does not exist on value of type 'JQuery'

  8. 8

    Property 'value' does not exist on type 'Element' angular

  9. 9

    JSDoc: Property 'value' does not exist on type 'EventTarget'

  10. 10

    Property 'value' does not exist on type Element

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

    The property 'xyz' does not exist on value of [imported] type 'xyz' in Typescript

  16. 16

    HTML Element - Does "value" attribute or property exist?

  17. 17

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

  18. 18

    property selectedIndex does not exists on type HTMLElement

  19. 19

    The default value type does not match the type of the property

  20. 20

    Uncaught TypeError: Cannot read property 'value' of null, Element does exist

  21. 21

    Uncaught TypeError: Cannot read property 'value' of null, Element does exist

  22. 22

    How to add default value if given property does not exist in lodash template

  23. 23

    Property does not exist on a function's return value of multiple types

  24. 24

    Property 'count' does not exist on type '{}'

  25. 25

    Property 'then' does not exist on type 'void'

  26. 26

    'Property does not exist on type 'never'

  27. 27

    Property 'json' does not exist on type '{}'

  28. 28

    Property push does not exist on type

  29. 29

    Angular - Property does not exist on type

HotTag

Archive