This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13248|Reply: 0

[TypeScript] TypeScript: Property "value" does not exist on type "HTMLElement"

[Copy link]
Posted on 5/27/2019 2:06:54 PM | | |
The html looks like this:

and greeter.ts Documents:

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

/home/bjarkef/sandbox/greeter.ts(8,53):属性’value’不存在类型’HTMLElement’的值,

However, the compiler outputs a javascript file, which works fine in chrome.

How did I get this error? How can I fix it?

Also, where can I find which properties are valid in "HTMLElement" according to typescript?

Note that I'm very new to JavaScript and typescript, so I might be missing something noticeable.


Based on Tomasz Nurkiewiczs' answer, the "problem" is that typescript is type-safe. :) so the type HTMLElement returned by document.getElementById() does not contain the value property. However, the HTMLInputElement subtype contains the value property.
So a solution is to convert the result of getElementById() to HTMLInputElement like this:


<> is the casting operator in the typescript.

The javascript generated from the above line looks like this:

That is, it does not contain type information.




Previous:"Container Cloud Operations in Practice: Docker and Kubernetes Clusters" by Huang Jingjun
Next:A certain horse go language blockchain employment class has ended
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com