Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using styled-components with props and TypeScript

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 * as React from "react"; import styled from "styled-components/native"; import { TouchableOpacity } from "react-native";  // -- types ----------------------------------------------------------------- // export interface Props {   onPress: any;   src: any;   width: string;   height: string; }  // -- styling --------------------------------------------------------------- // const Icon = styled.Image`   width: ${(p: Props) => p.width};   height: ${(p: Props) => p.height}; `;  class TouchableIcon extends React.Component<Props> {   // -- default props ------------------------------------------------------- //   static defaultProps: Partial<Props> = {     src: null,     width: "20px",     height: "20px"   };    // -- render -------------------------------------------------------------- //   render() {     const { onPress, src, width, height } = this.props;     return (       <TouchableOpacity onPress={onPress}>         <Icon source={src} width={width} height={height} />       </TouchableOpacity>     );   } }  export default TouchableIcon; 

Following line throws 3 errors, that are same in nature <Icon source={src} width={width} height={height} />

Type {source: any; width: string; height: string;} is not assignable to type IntrinsicAttributes ... Property 'onPress' is missing in type {source: any; width: string; height: string;}

Not entirely sure what this is and how to fix it, do I somehow need to declare these on Icon or something of this sort?

EDIT: typescript v2.6.1, styled-components v2.2.3

like image 512
Ilja Avatar asked Nov 02 '17 14:11

Ilja


People also ask

Can you use styled-components with TypeScript?

We need to install some dependencies because styled-components doesn't come with create-react-app. This installs the styled-components types for TypeScript as a dev dependency. We're also going to use Material Icons, so let's install the material-ui package .

Can you use props in styled-components?

To pass props to React components created with styled-components, we can interpolate functions into the string that creates the component. We create the ArrowStyled component with the styled. div tag. Then string that we pass into the tag has the rotate value set from a prop.

Can styled-components receive custom props?

The css prop is a convenient way to iterate on your components without settling on fixed component boundaries yet. It works on both normal HTML tags as well as components, and supports everything any styled component supports, including adapting based on props, theming and custom components.


1 Answers

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<Props>`   width: ${p => p.width};   height: ${p => p.height}; `; 

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

const Icon = styled.Image<Pick<Props, 'src' | 'width' | 'height'>>`   width: ${p => p.width};   height: ${p => p.height}; `; 
like image 161
elnygren Avatar answered Sep 19 '22 14:09

elnygren