Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript form reset() not working

I'm using typescript to reset a form but it's not working or typescript compiler (1.0.3 version) doesn't recognize reset() function. Compiler gives Error

Build: Interface 'HTMLFormElement' incorrectly extends interface 'HTMLElement'. C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.4\lib.d.ts

This is the typescript code

var resetForm =document.getElementById(dirtyFormID);
resetForm.reset();

When I copied above code to js file, it's working perfectly.

What's the reason for it?

like image 248
cp100 Avatar asked Nov 30 '22 17:11

cp100


2 Answers

Since the function getElementById returns a more generic type HTMLElement you need to assert the specific version manually :

var dirtyFormID = 'something';
var resetForm = <HTMLFormElement>document.getElementById(dirtyFormID);
resetForm.reset();
like image 67
basarat Avatar answered Dec 04 '22 08:12

basarat


With typescript.

to avoid Google Chrome Error "resetForm.reset is not a function", I specified the type of my button element, "reset" in my HTML code. Having specified a:

type = 'reset'

make my function works very well.

like image 25
Unicall Avatar answered Dec 04 '22 07:12

Unicall