0

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.

Is there a way to use .value, reset() and other dom methods or at least make compiler ignore this error?

Lukáš Václavek
  • 442
  • 1
  • 6
  • 12

1 Answers1

2

For document.getElementById(id), TypeScript can't know for sure that the element returned will of the type 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):

return (document.getElementById(id) as HTMLInputElement).value
basarat
  • 261,912
  • 58
  • 460
  • 511