简体   繁体   English

如何在打字稿中使用reset()、. value和其他DOM键?

[英]How to use reset(), .value, and other DOM keys in typescript?

So I'm using typescript for basic funkcionality, however it refuses to compile because of the following error 所以我将打字稿用于基本功能,但是由于以下错误,它拒绝编译

src/app.ts:46:38 - error TS2339: Property 'value' does not exist on type 'HTMLElement'.

46   return document.getElementById(id).value

I have es2017 as a target, therefore I have read that it includes dom libraries, however I still cannot make it work. 我将es2017作为目标,因此我已经阅读到它包含dom库,但是我仍然无法使其工作。

Is there a way to use .value, reset() and other dom methods or at least make compiler ignore this error? 有没有办法使用.value,reset()和其他dom方法,或者至少使编译器忽略此错误?

For document.getElementById(id) , TypeScript can't know for sure that the element returned will of the type HTMLInputElement . 对于document.getElementById(id) ,TypeScript无法确定返回的元素将是HTMLInputElement类型。 You will have to use an assertion if you want to infor typescript about what your html will contain (and therefore dom will return): 如果您想为您的html包含什么打字稿信息(因此dom将返回),则必须使用一个断言

return (document.getElementById(id) as HTMLInputElement).value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM