Jump to content
Search Community

typescript errors in gsap.matchMedia()

BenjaminO test
Moderator Tag

Go to solution Solved by BenjaminO,

Recommended Posts

I have the following typescript errors for isMobile and isDesktop in gsap.matchMedia():

 

const isDesktop: any

property 'isDesktop' doesn't exists on type 'Conditions | undefined'.ts(2339)

 

  const mm = gsap.matchMedia()
  mm.add(
    { isMobile: '(max-width: 766px)', isDesktop: '(min-width: 767px)' },
    (context) => {
      const { isMobile, isDesktop } = context.conditions // error ts 2339
    }
  )

 

Link to comment
Share on other sites

Hi 

 

I'm far from being a TS expert or even competent at it, but in your code snippet where are you passing the types declaration for isMobile and isDesktop?

 

Those should be booleans as far as I can tell.

 

Right now I'm using my phone so I can't check, but I'm pretty sure that you can create a TS example in either codesanbox or replit.com and set up a minimal demo that we can take a look at.

 

Happy Tweening!

Link to comment
Share on other sites

@BenjaminO that is typed like this in the definitions file: 

interface Conditions {
  [key: string]: boolean;
}

Which as I understand it allows for ANY key. Am I missing something? I'm not aware of any other way to do this, but let us know if you have a suggestion.  Keep in mind that isDesktop and isMobile are completely arbitrary names - users can pick any name they want for any condition, so we cannot pre-define any particular names on the Conditions object. 

  • Like 1
Link to comment
Share on other sites

I think the problem is related to the interface Context below :

  interface Context {
    [key: string]: any;
    selector?: Function;
    isReverted: boolean;
    conditions?: Conditions; // no problem without the ?
    queries?: object;
    add(methodName: string, func: Function, scope?: Element | string | object): Function;
    add(func: Function, scope?: Element | string | object): void;
    ignore(func: Function): void;
    kill(revert?: boolean): void;
    revert(config?: object): void;
    clear(): void;
  }

The thing is that the different conditions (isMobile, isDesktop in my case) are typed ANY because they inherit from a union type of gsap.Conditions | undefined because of the ?. The undefined type is causing the error here so conditions should be mandatory IMHO.

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