【问题标题】:react-pose: How do I define the types for props in react-pose? (props implicitly has any type)react-pose:如何在 react-pose 中定义道具的类型? (道具隐含任何类型)
【发布时间】:2019-05-09 13:20:14
【问题描述】:

我正在使用带有react-pose 的 TypeScript,并且需要定义道具的类型。这是我刚开始使用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
  }
});

我在这里错过了什么?

【问题讨论】:

    标签: reactjs typescript react-pose


    【解决方案1】:

    为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
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多