1

I'm building a React app using TypeScript. I'm using React-Slick's carousel.

I'm trying to programmatically change the slide of the carousel. Therefore I followed the documentation and tried to create a ref for the Slider.

<Slider ref={slider => (this.slider = slider)} {...settings}>

Now TypeScript complains that it does not now slider. So I tried declaring it like this:

slider: Slider | null;

If I try it like this, I get the error:

[ts] Property 'slider' has no initializer and is not definitely assigned in the constructor.

How is one supposed to make a ref work with TypeScript?

The only way I could find was to say:

slide: any;

which defeats the purpose of TypeScript .

EDIT: Tolle you marked this question as a duplicate. But it is different, in that here the ref is created this way:

ref={slider => (this.slider = slider)}

And in your proposed original question this way:

ref={this.stepInput}
J. Hesters
  • 13,117
  • 31
  • 133
  • 249

0 Answers0