P
P
Paxest2020-02-03 11:13:45
typescript
Paxest, 2020-02-03 11:13:45

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');


Typescript swears that you this.singleSlider[offset]can’t access the value in HTMLElement like this because there is no key with type string in the element

Element 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'.


If set for offset: any, then
Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'HTMLElement'.ts(7053)


Tell me, please, what type to set for offset in order to correctly refer to the value inside the HTMLElement?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-02-03
@Paxest

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 question

Ask a Question

731 491 924 answers to any question