Anthony Kong
Anthony Kong

Reputation: 40624

"TS7017: Element implicitly has an 'any' type ... has no index signature" when I use react-transition-group

Here is my code:

 renderSoundWave = () => {
    const defaultStyle = {
      opacity: 1,
      transition: `opacity ${DURATION}ms ease-in-out`,
    }

    const transitionStyles = {
      entering: { opacity: 1 },
      entered:  { opacity: 0 },
    };
    return (

    <Transition timeout={DURATION} in={this.animate}>
      {(state) => (
        <div className={styles.soundWaves}
             style={{ ...defaultStyle, ...transitionStyles[state]}}> {/* Error here! */
          <SoundWaves/>
        </div>
        )}
    </Transition>
      );
  }

I want to use Transition in react-transition-group to animate the icon SoundWave.

However I am getting this error:

error TS7017: Element implicitly has an 'any' type because type '{ entering: { opacity: number; }; entered: { opacity: number; }; }' has no index signature.

The error points to ...transitionStyles[state] above

I don't understand why this error is thrown. What is causing this type error?

Upvotes: 1

Views: 822

Answers (1)

Anthony Kong
Anthony Kong

Reputation: 40624

I finally fixed it by changing transitionStyles to

const transitionStyles: { [id: string]: React.CSSProperties } = {
  entering: { opacity: 1 },
  entered:  { opacity: 0 },
};

Upvotes: 5

Related Questions