Answer the question
In order to leave comments, you need to log in
What type should be the variable that is passed as a key to access the value that is in the HTMLElement?
Good afternoon!
Optimizing repetitive code, wrote a function.
const move = (direction: string, offset: string) => {
if (this.sliderType === 'single') {
this.elem.querySelectorAll('.iss__tooltip')[0].style[direction] = `${this.singleSlider[offset]}px`;
}
}
move('left', 'offsetLeft');
this.singleSlider[offset]
can’t access the value in HTMLElement like this because there is no key with type string in the elementElement implicitly has an 'any' type because expression of type 'string' can't be used to index type 'HTMLElement'.
No index signature with a parameter of type 'string' was found on type 'HTMLElement'.
Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'HTMLElement'.ts(7053)
Answer the question
In order to leave comments, you need to log in
offset and singleSlider must be normally typed.
offset at least as keyof HTMLElement
, but in general a list of possible values is better.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question