Jump to content
Search Community

TypeScript types of gsap.utils.toArray elements with scrollTrigger

Yah-Yah test
Moderator Tag

Recommended Posts

I set up simple animation on scroll up, it's an Array of elements that share classname '.slide-up' and the animation is triggered when each of the elements enters the viewport.

    gsap.utils.toArray('.slide-up').forEach((elem: any) => {
      gsap.fromTo(
        elem,
        // elem! as gsap.DOMTarget,
        {
          y: 100,
          opacity: 0,
        },
        {
          scrollTrigger: {
            trigger: elem,
            start: '-80px bottom',
          },
          duration: 2,
          ease: 'expo.out',
          y: 0,
          opacity: 1,
        }
      );
    });

 

All is fine except for the types I had to assign, namely I can only run this when the 'elem' has type 'any'. I tried assigning other, more fitting types such as 'HTMLDivElement', but then TS complains that 

Argument of type '(elem: HTMLDivElement) => void' is not assignable to parameter of type '(value: unknown, index: number, array: unknown[]) => void'.
Types of parameters 'elem' and 'value' are incompatible.
Type 'unknown' is not assignable to type 'HTMLDivElement'.ts(2345)

If I try casting it as unknown, then the scrollTrigger throws a fuss about it:

Type 'unknown' is not assignable to type 'DOMTarget | undefined'.
  Type 'unknown' is not assignable to type 'ArrayLike<string | Element | null>'.ts(2322)

 

I'm really confused here and my code won't get past code review with type ':any' so I have to find solution.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...