"TS7017: Element implicitly has an 'any' type ... has no index signature" 当我使用 react-transition-group
"TS7017: Element implicitly has an 'any' type ... has no index signature" when I use react-transition-group
这是我的代码:
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>
);
}
我想在 react-transition-group
中使用 Transition
为图标 SoundWave
设置动画。
但是我收到这个错误:
error TS7017: Element implicitly has an 'any' type because type '{
entering: { opacity: number; }; entered: { opacity: number; }; }' has
no index signature.
错误指向
上面的 ...transitionStyles[state]
我不明白为什么会抛出这个错误。是什么导致了这种类型错误?
我最终通过将 transitionStyles
更改为
来修复它
const transitionStyles: { [id: string]: React.CSSProperties } = {
entering: { opacity: 1 },
entered: { opacity: 0 },
};
这是我的代码:
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>
);
}
我想在 react-transition-group
中使用 Transition
为图标 SoundWave
设置动画。
但是我收到这个错误:
error TS7017: Element implicitly has an 'any' type because type '{ entering: { opacity: number; }; entered: { opacity: number; }; }' has no index signature.
错误指向
上面的...transitionStyles[state]
我不明白为什么会抛出这个错误。是什么导致了这种类型错误?
我最终通过将 transitionStyles
更改为
const transitionStyles: { [id: string]: React.CSSProperties } = {
entering: { opacity: 1 },
entered: { opacity: 0 },
};