I'd like to access form elements via myForm.elements
, and then access each element by it's name, for example, myForm.elements.month
. Typescript doesn't like this b/c it doesn't know that form.elements
contains a property of month
. I thought, let's create an interface! So I did, (see code below), but I'm getting this typescript error: Neither type 'HTMLCollection' nor type 'FormElements' is assignable to the other
Here's the code I'm working with:
interface FormElements {
day: HTMLInputElement;
month: HTMLInputElement;
year: HTMLInputElement;
}
class BirthdateInput {
constructor(form: HTMLFormElement) {
var elements: FormElements = <FormElements> form.elements; // error here
this.day = elements.day;
this.month = elements.month;
this.year = elements.year;
}
}
Any ideas on how to better cast my form.elements
object so typescript won't complain?
FormElements
actually corresponds to form.elements, not the form itself, so it's better to extend fromHTMLCollection
, or possiblyHTMLFormControlsCollection
thanHTMLFormElement
. But theextends
aspect is correct. – Lafreniere