question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Add support for styled-components-ts

See original GitHub issue

There is an issue with styled-components and typescript where typing props inside intropolations is hard because of an issue where you can’t pass generic information to tagged template functions:

https://github.com/Microsoft/TypeScript/issues/11947

https://github.com/styled-components/styled-components/issues/1428

Which is a huge annoyance compared to styled-components otherwise excellent ts support The current worked around is to use another library called styled-components-ts which does this:

// Import react, styledComponents and styledComponentsTS
import * as React from 'react';
import styledComponents from 'styled-components';
import styledComponentsTS from 'styled-components-ts';

// Create an interface for the component
export interface MyImageProps {
  size: number;
  borderColor?: string;
  borderSize?: number;
}

// Create a styled component with our props interface
const MyImage = styledComponentsTS<MyImageProps>(styledComponents.img) `
  width: ${props => props.size}px;
  height: ${props => props.size}px;
  border: ${props => props.borderSize || '4px'} solid ${props => props.borderColor || 'black'}
`;

export default MyImage;

Properly typing the props options in the interpolations without any other config. As TS issue 11947 doen’t seem to be getting fixed anytime soon (At least until the problem affects other library other then styled-components) we are stuck with it for some time.

Of course right now, using 'styledComponentsTs()` breaks syntax highlighting and intellisence inside the template literal, believing it to be a normal template literal. I was wondering if this could be fixed, as I think styled-components-ts is almost essencal to using styled-components and typescript together and vscode is obviously very popular among typescript devs.

Otherwise thanks for the good work.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
beshanoecommented, Apr 21, 2018

No need for supporting styled-component-ts anymore, passing generics to tagged templates has landed in typescipt@next and headed to 2.9: https://github.com/Microsoft/TypeScript/issues/11947#issuecomment-383252975 now this extension should definitely support such code:

export const RedBox = styled.div<{foo: string}>`
  background: red;
  ${props => props.foo}
`;
1reaction
Cu3PO42commented, Apr 22, 2018

Thanks for the heads up @beshanoe. I have opened #81 to track support for that syntax. In light of this, I believe support for styled-components-ts will not be worked in since in will be made obsolete anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference - styled-components
It works on both normal HTML tags as well as components, and supports everything any styled component supports, including adapting based on props,...
Read more >
tailwind-styled-components - npm
Create tailwind css react components like styled components with classes name on multiple lines. Latest version: 2.2.0, last published: 4 ...
Read more >
A good feature request collection tool helps product managers ...
The right collection tool will be able to organize all of your customers' requested features easily, combining duplicate/similar suggestions on ...
Read more >
react-styled-flex | Yarn - Package Manager
Supports SSR and SSG rendering. Introduces Box component. Uses styled-components shouldForwardProp mechanism to avoid leaking props to DOM. As a result, is prop ......
Read more >
Create a killer Frontend for 2020 | How to setup Next.js, Apollo ...
The displayName option adds the component name to the generated CSS class to make it easier for debugging. preprocess is an experimental feature...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found