Typing dynamic template refs in a loop
So I was working on some unrelated type refactoring and once I fixed some typing issue, another one arrised. And since it is a little bit non-standard code from around 2 years ago, I wonder how to go about it.
So basically we have a carousel like this (I simplified it a bit and omitted unrelated props)
As you can see we are assigning refs to elements rendered in a loop (they are slotted into a component that renders them in a loop).
And inside the component I'm working on we are getting an active index as a prop:
Then using said index we are getting ref of a current item via a function:
And using this active ref for example like this:
or like this:
Apparently it is working fine. At least at runtime. But typescript has a problem with it saying:
Sooo... now I'm not sure how to proceed. Do you have an idea?
1 Reply
I could just slap the
as
keyword:
And red squiggly lines will magically go away. But I'm not sure if it is really correct typing and anyway I want to avoid using as
as much as possible (pun not intended).
Do you have an idea how to overcome it? What would be a good refactor to make here?