Using styled components with props and typescript

后端 未结 4 1963
眼角桃花
眼角桃花 2021-02-01 20:24

I\'m trying to integrate typescript into our project and so far I stumbled upon one issue with styled-components library

Consider this component

import *         


        
4条回答
  •  有刺的猬
    2021-02-01 20:43

    There have been some recent developments and with a new version of Typescript (eg. 3.0.1) and styled-components (eg. 3.4.5) there's no need for a separate helper. You can specify the interface/type of your props to styled-components directly.

    interface Props {
      onPress: any;
      src: any;
      width: string;
      height: string;
    }
    
    const Icon = styled.Image`
      width: ${p => p.width};
      height: ${p => p.height};
    `;
    

    and if you want to be more precise and ignore the onPress

    const Icon = styled.Image>`
      width: ${p => p.width};
      height: ${p => p.height};
    `;
    

提交回复
热议问题