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};
`;
Answer from elnygren on Stack Overflow
🌐
npm
npmjs.com › package › @types › styled-components
types/styled-components
This package contains type definitions for styled-components (https://github.com/styled-components/styled-components).
      » npm install @types/styled-components
    
🌐
styled-components
styled-components.com › docs › api
styled-components
import type { CSSProperties, // Standard CSS property/value pairs CSSObject, // Object styles with nested selectors CSSPseudos, // Pseudo-class and pseudo-element selectors CSSKeyframes, // Keyframe animation definitions } from 'styled-components'; CSS custom properties (--var) also work without TypeScript errors in .attrs() and JSX style props as of v6.3.0.
Discussions

reactjs - TypeScript with styled-components - Stack Overflow
TypeScript newbie here. I have a below component using styled-components that I would like to convert to TypeScript. import React from 'react' import PropTypes from 'prop-types' import styled from ' More on stackoverflow.com
🌐 stackoverflow.com
typescript - How to add types do styled components - Stack Overflow
I am making tooltip using antd and I would like to add props leftto styled components. I am getting following error when I hover left prop in styled components: Property 'left' does not exist on ty... More on stackoverflow.com
🌐 stackoverflow.com
June 13, 2022
How do I provide props to styled elements in Typescript?
I looked at the docs and other than creating a whole empty component purely for providing a definition of props I'm not sure of if it's possible to provide custom props to a styled element ... More on github.com
🌐 github.com
100
March 30, 2017
`as` prop of styled components in TypeScript
When using the styled component pass the as prop from Emotion 10 to change the element being used. ... I've created a code sandbox for this, but I don't think the setup there properly reflects mine. Since you can't seem to customize the tsconfig in the default Typescript + React starter, I ... More on github.com
🌐 github.com
18
December 28, 2018
🌐
LogRocket
blog.logrocket.com › home › using styled-components in typescript: a tutorial with examples
Using styled-components in TypeScript: A tutorial with examples - LogRocket Blog
June 4, 2024 - We’re also going to use Material Icons, so let’s install the material-ui package. Type the following and press enter: ... There’s a styled-components extension for VS Code that makes it look like we’re typing actual CSS even though it’s a TypeScript file.
🌐
SaltyCrane
saltycrane.com › cheat-sheets › typescript › styled-components › latest
TypeScript styled-components cheat sheet - SaltyCrane
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. This is a list of TypeScript types for styled-components generated from the declaration files in https://github.com/DefinitelyTyped/DefinitelyTyped/tree/db7577c.
🌐
CodeVertiser
codevertiser.com › styled-components-typescript-prop-types-issue
How To Fix Styled Components Prop Types Issue In Typescript
July 22, 2022 - Let's say we are building a reusable Button component in react, button accepts 3 props, title, variant, and onClick. Based on variant we define button background color, either primary or secondary. ... Typescript will keep yelling unless we define style types for variant. To fix this we define a separate type for our Button Style.
🌐
Medium
medium.com › @luxtars.dev › use-styled-components-with-typescript-73b703261d9e
Use styled-components with TypeScript | by Luxtars Dev | Medium
April 8, 2020 - As I’m transitioning most of my projects to include TypeScript, there are things I stumbled along, but there are things that feel perfect. Listing down some of them here. Styled Components library does not ship with types. Instead, we have ...
Find elsewhere
🌐
GitHub
github.com › styled-components › styled-components › blob › main › packages › styled-components › src › types.ts
styled-components/packages/styled-components/src/types.ts at main · styled-components/styled-components
* To enable support for the `css` prop in TypeScript, create a `styled-components.d.ts` file in · * your project source with the following contents: * * ```ts · * import type { CSSProp } from "styled-components"; * * declare module "react" { * interface Attributes { * css?: CSSProp; * } * } * ``` * * In order to get accurate typings for `props.theme` in `css` interpolations, see ·
Author   styled-components
Top answer
1 of 3
12
import React from "react";
import styled from "styled-components";

interface Props {
  name: string;
  className?: string;
}

const NonStyledIcon: React.SFC<Props> = ({ name, className, ...props }) => (
  <i className={`material-icons ${className}`} {...props}>
    {name}
  </i>
);

const Icon = styled(NonStyledIcon)`
  font-size: ${props => props.theme.sizeLarger};
`;

export default Icon;

As per the styled-components TypeScript docs: when defining a component you will need to mark className as optional in your Props interface

2 of 3
6

Here's a comprehensive approach using Styled Components v5 with React Native that will work with plain React as well. If you're not using a theme you can skip to the StyledProps section at the bottom.

  • Define your theme type.

    // MyTheme.ts
    
    export type MyTheme = {
      colors: {
        primary: string;
        background: string;
      };
    };
    
  • Use the type on your themes.

    // themes.ts
    
    export const LightTheme: MyTheme = {
      colors: {
        primary: 'white',
        background: 'white',
      },
    };
    
    export const DarkTheme: MyTheme = {
      colors: {
        primary: 'grey',
        background: 'black',
      },
    };
    
  • Use declaration merging to "merge" the MyTheme type into Styled Components default theme.

    // styled.d.ts
    
    import 'styled-components';
    import { MyTheme } from '../src/themes/MyTheme';
    
    declare module 'styled-components' {
      // eslint-disable-next-line @typescript-eslint/no-empty-interface
      export interface DefaultTheme extends MyTheme {}
    }
    

OK, cool. The theme prop is correctly typed.
What about the components themselves?

  • Wrap your specific component props in the StyledProps type.

    import { StyledProps } from 'styled-components';
    import styled from 'styled-components/native';
    
    type MyViewProps = StyledProps<{
      backgroundColor?: string;
      isAlert?: boolean;
    }>;
    
    const MyView = styled.View(
      (props: MyViewProps) => `
        background-color: ${props.backgroundColor || props.theme.colors.background};
        color: ${props.isAlert ? red : props.theme.colors.primary}
      `,
    );
    

In this example both props.backgroundColor and props.theme.colors.background will auto-complete. When you update MyTheme type or the specific component type it should just work.

🌐
Emotion
emotion.sh › docs › typescript
Emotion – TypeScript
It's not possible to leverage css prop support being added conditionally based on the type of a rendered component when not using our jsx pragma or the react-jsx transform. If you use our pragma implicitly (for example when using our @emotion/babel-preset-css-prop) we have a special file that can be imported once to add support for the css prop globally, for all components. Use it like this: /// <reference types="@emotion/react/types/css-prop" /> @emotion/styled works with TypeScript without any additional configuration.
🌐
Newline
newline.co › @kchan › annotating-react-styled-components-with-typescript--e8076d1d
Annotating React Styled Components with TypeScript | newline
November 24, 2021 - Styled components redefine how we apply CSS styles to React components. Unlike the traditional approach of manually assigning CSS classes (from an imported CSS file) to elements within a component, CSS-in-JS libraries like styled-components ...
🌐
DEV Community
dev.to › griseduardo › styled-components-in-react-with-typescript-nfj
Styled-components in React with Typescript - DEV Community
December 26, 2024 - If $fontSize is provided, it sets the font size; otherwise, it defaults to 16px. Thus, the component is defined using the lib. In the example, the name StyledText is used, but the name is customizable. Starting from the structure defined above in React structure, here is the format with the type definitions:
🌐
DEV Community
dev.to › rajuashok › create-styled-d-ts-to-make-typescript-work-with-styled-components-5dk4
Create styled.d.ts to make Typescript work with styled-components - DEV Community
September 19, 2020 - import {} from 'styled-components'; import { ThemeType } from './theme'; // Import type from above file declare module 'styled-components' { export interface DefaultTheme extends ThemeType {} // extends the global DefaultTheme with our ThemeType. } That's it! You're typescript compiler and VSCode should stop complaining about props.theme.primary.
🌐
GitHub
github.com › styled-components › styled-components › issues › 630
How do I provide props to styled elements in Typescript? · Issue #630 · styled-components/styled-components
March 30, 2017 - I looked at the docs and other than creating a whole empty component purely for providing a definition of props I'm not sure of if it's possible to provide custom props to a styled element other than theme. Example For the above example ...
Author   tal
🌐
GitHub
github.com › emotion-js › emotion › issues › 1137
`as` prop of styled components in TypeScript · Issue #1137 · emotion-js/emotion
December 28, 2018 - // Custom styled function that is typed with my theme interface import styled, { CreateStyled } from '@emotion/styled' import { ITheme } from '../theme' export default styled as CreateStyled<ITheme> // Style function interface in a Util module declare module 'Util' { import { css } from '@emotion/core' import { ITheme } from '../util/theme' type CssType = typeof css export interface IStyleFunction<P = {}> { ( arg0: { theme: ITheme } & P): ReturnType<CssType> | false | null } } // Component code type HeadingProps = { size?: FontSizes } const baseStyles: IStyleFunction<HeadingProps> = ({ theme }
Author   felixjung
🌐
Medium
medium.com › @binyamin › using-typescript-with-styled-components-35950e196e9c
Using TypeScript with Styled-Components | by Ben Grunfeld | Medium
June 1, 2020 - Basically, if you’re using a prop inside of Styled Components that has a type definition set, you have to provide a type definition for it in your Component.styles.ts file too, as I’ve done in this example:
🌐
GitHub
github.com › hatashiro › typed-styled-components
GitHub - hatashiro/typed-styled-components: TypeScript + styled-components in a simple way
May 4, 2020 - There is already styled-components-ts for using styled-components with TypeScript. The difference is just how prop types are specified, so the choice is yours. One major difference is that typed-styled-components has styled-components as its dependency and exports everything from it, so that you don't need to install both.
Author   hatashiro
🌐
Reddit
reddit.com › r/typescript › what is the type of the styled-components props in typescript?
r/typescript on Reddit: What is the type of the styled-components props in TypeScript?
January 14, 2023 -

I am trying to do this:

import React from 'react'
import styled, { CSSProperties, useTheme } from 'styled-components'
import { Theme } from './styles'
import useProps from './useProps'

export type BasicInputPropsType<T> = T | Array<T>

export type FactoryStyledPropsType = {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  styledProps: any
}

export type InputPropsFunctionType<I, T> = (props: I) => T

export type InputPropsType<T, I> = T extends Function
  ? InputPropsFunctionType<I, T>
  : T

export default function FactoryFactory<T>(
  as: string,
  serializer: (theme: Theme, props: T) => CSSProperties,
) {
  return function Factory<I>(inputProps: InputPropsType<T, I>) {
    const isPropsFunction = typeof inputProps === 'function'
    const Styled = styled.div<FactoryStyledPropsType>(
      props => props.styledProps,
    )

    function Component(props: I) {
      const actualProps = isPropsFunction
        ? (inputProps as InputPropsFunctionType<I, T>)(props)
        : props
      const theme = useTheme()

      const { styledProps, elementProps } = useProps(
        actualProps,
        theme,
        serializer,
      )

      return (
        <Styled
          {...elementProps}
          styledProps={styledProps}
        />
      )
    }

    Component.toString = Styled.toString

    return Component
  }
}

Essentially that styledProps, it should return what the styled component expects, but I can't figure out how to type it. I am looking at this type definition:

export interface ThemedStyledFunctionBase<
    C extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
    T extends object,
    O extends object = {},
    A extends keyof any = never
> {
    (first: TemplateStringsArray): StyledComponent<C, T, O, A>;
    (
        first:
            | TemplateStringsArray
            | CSSObject
            | InterpolationFunction<ThemedStyledProps<StyledComponentPropsWithRef<C> & O, T>>,
        ...rest: Array<Interpolation<ThemedStyledProps<StyledComponentPropsWithRef<C> & O, T>>>
    ): StyledComponent<C, T, O, A>;
    <U extends object>(
        first:
            | TemplateStringsArray
            | CSSObject
            | InterpolationFunction<ThemedStyledProps<StyledComponentPropsWithRef<C> & O & U, T>>,
        ...rest: Array<Interpolation<ThemedStyledProps<StyledComponentPropsWithRef<C> & O & U, T>>>
    ): StyledComponent<C, T, O & U, A>;
}

But that is way too complex, I am not sure what to do. What is the type I need to specify for my styledProps?