Hey,
can someone please help me to avoid/suppress theses errors? I guess I need to add annotations or change the .flowconfig?
document.querySelector('div#someID > select#anotherID').value = 'newValue';
property value
Property not found in HTMLElement
let selects = document.getElementsByTagName('select');
selects.anotherID.value = 'newValue';
property anotherID
Property not found in HTMLCollection
The problem is that document.querySelector
returns a HTMLElement, which does not necessarily have the property value. You can test for the type of the returned value, by doing, for instance:
const el = document.querySelector('div#someID > select#anotherID')
if (el instanceof HTMLInputElement)
el.value = 'newValue'
You can also cast the returned value of querySelector
to be any
, by doing something like (document.querySelector('div#someID > select#anotherID'): any).value = 'newValue'
.
But you'll lose all the type checks related to this section.
@logol I think in this case the declaration of querySelector
is wrong. querySelector
can return null, or HTMLElement or subtypes of HTMLElement.
Edit: it looks like this is the case from many of the declarations in flow related to the DOM APIs.
Ah, that explained a lot - thank you very much for your feedback @aackerman @AugustinLF
Most helpful comment
The problem is that
document.querySelector
returns a HTMLElement, which does not necessarily have the property value. You can test for the type of the returned value, by doing, for instance:You can also cast the returned value of
querySelector
to beany
, by doing something like(document.querySelector('div#someID > select#anotherID'): any).value = 'newValue'
.But you'll lose all the type checks related to this section.