Windows 7 x64
Compiling using gulp-typescript 2.7.7 using typescript 1.5 beta
Getting error: Property 'classList' does not exist on type 'Node' because of statement
document.getElementsByClassName('left-aside-wrapper')[0].classList.toggle('isOpen');
I see that classList is defined in the lib.d.ts file as
interface Element extends Node, GlobalEventHandlers, ElementTraversal, NodeSelector, ChildNode {
classList: DOMTokenList;
Not all that familiar with how d.ts files work so is this my error or is it a bug in the d.ts file that need to be reported?
I have got the same issue when I want to update the classList of the fist child.
And my solution is
const element = document.getElementById('testId').firstChild as HTMLElement;
Use firstElementChild
instead of firstChild
(i.e. instead of getElementsByClassName('')[0]
)
So
document.getElementsByClassName('left-aside-wrapper').firstElementChild.classList.toggle('isOpen')
This is becuase getElementsByClassName
is defined to return Node
array and not an Element
array.
Not all that familiar with how d.ts files work so is this my error or is it a bug in the d.ts file that need to be reported
Based on https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName I would report it as an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With