我正在将TypeScript与react-pose一起使用,并且需要定义道具的类型。这是我刚开始使用create-react-app
3.0.1版的应用程序。
我不断收到错误Parameter 'props' implicitly has an 'any' type.
我尝试过在div上定义类似于styled-components
的类型:
const FadeIn = posed.div<{ duration: number; hiddenOffset: number }>({
visible: {
opacity: 1,
scale: 1,
x: 0,
y: 0,
transition: { duration: props => props.duration }
},
hidden: {
opacity: 0,
scale: 0.8,
x: props => props.hiddenOffset,
y: 10
}
});
我还尝试定义一个组件来代替posed.div
:
const FadeInContainer: React.FC<{
duration: number;
hiddenOffset: number;
}> = props => {
return <div {...props} />;
};
并通过它:
const FadeIn = posed(FadeInContainer)({
visible: {
opacity: 1,
scale: 1,
x: 0,
y: 0,
transition: { duration: props => props.duration }
},
hidden: {
opacity: 0,
scale: 0.8,
x: props => props.hiddenOffset,
y: 10
}
});
我在这里想念什么?
答案 0 :(得分:0)
为props
定义接口并注释函数自变量似乎可行。
interface Props {
duration: number;
hiddenOffset: number;
}
const FadeIn = posed.div({
visible: {
opacity: 1,
scale: 1,
x: 0,
y: 0,
transition: (props: Props) => ({ duration: props.duration })
},
hidden: {
opacity: 0,
scale: 0.8,
x: (props: Props) => props.hiddenOffset,
y: 10
}
});