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.

[TS] Proposal: add helper method for adding types to props

See original GitHub issue

Hi everyone!

I been seeing a lot of frustration on declaring components with TypeScript, especially with stateless ones. It is quite hard/repetitive to add an interface to the props. So I was thinking that we can provide an helper function for this, something along the lines of:


import styled from "styled-components";

interface ImageProps {
  fade: boolean;
}

const Img =styled.img.ts.withInterface<ImageProps>()`
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  object-fit: contain;
  max-width: 100%;
  max-height: 100%;
  border-radius: 3px;
  opacity: ${(props) => props.fade ? 0.3 : 1};
`;

It is a bit ugly but it could work. @Igorbek what do you think?

PS. This will be solved when this issue gets fixed: https://github.com/Microsoft/TypeScript/issues/11947 but in the meanwhile this proposal can be a good workaround.

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
clayne11commented, Jun 8, 2017

The alternative is defining the use a string inside of styled() such as styled('div'). We can augment the style definitions like so:

export interface ThemedBaseStyledInterface<T> {
  <P>(component: Component<P> | ComponentString): ThemedStyledFunction<P, T>;
}

export type ComponentString = (
  'a' |
  'abbr' |
  'address' |
  'area' |
  'article' |
  'aside' |
  'audio' |
  'b' |
  'base' |
  'bdi' |
  'bdo' |
  'big' |
  'blockquote' |
  'body' |
  'br' |
  'button' |
  'canvas' |
  'caption' |
  'cite' |
  'code' |
  'col' |
  'colgroup' |
  'data' |
  'datalist' |
  'dd' |
  'del' |
  'details' |
  'dfn' |
  'dialog' |
  'div' |
  'dl' |
  'dt' |
  'em' |
  'embed' |
  'fieldset' |
  'figcaption' |
  'figure' |
  'footer' |
  'form' |
  'h1' |
  'h2' |
  'h3' |
  'h4' |
  'h5' |
  'h6' |
  'head' |
  'header' |
  'hgroup' |
  'hr' |
  'html' |
  'i' |
  'iframe' |
  'img' |
  'input' |
  'ins' |
  'kbd' |
  'keygen' |
  'label' |
  'legend' |
  'li' |
  'link' |
  'main' |
  'map' |
  'mark' |
  'menu' |
  'menuitem' |
  'meta' |
  'meter' |
  'nav' |
  'noscript' |
  'object' |
  'ol' |
  'optgroup' |
  'option' |
  'output' |
  'p' |
  'param' |
  'picture' |
  'pre' |
  'progress' |
  'q' |
  'rp' |
  'rt' |
  'ruby' |
  's' |
  'samp' |
  'script' |
  'section' |
  'select' |
  'small' |
  'source' |
  'span' |
  'strong' |
  'style' |
  'sub' |
  'summary' |
  'sup' |
  'table' |
  'tbody' |
  'td' |
  'textarea' |
  'tfoot' |
  'th' |
  'thead' |
  'time' |
  'title' |
  'tr' |
  'track' |
  'u' |
  'ul' |
  '"var"' |
  'video' |
  'wbr' |
  'circle' |
  'clipPath' |
  'defs' |
  'ellipse' |
  'g' |
  'image' |
  'line' |
  'linearGradient' |
  'mask' |
  'path' |
  'pattern' |
  'polygon' |
  'polyline' |
  'radialGradient' |
  'rect' |
  'stop' |
  'svg' |
  'text' |
  'tspan'
)

Then we can call:

interface MyProps

const StyledWithProps = styled<MyProps>('div')`
`
2reactions
flexzuucommented, Feb 13, 2018

@clayne11 I understand this approach but i get a implicit any warning for the arguments passed to the functions inside the tagged literal. Is there anything else i need to to to get this working?

I was thinking about building a monkey patch that converts the functions on styled components like styled.div to a higher oder function like styled.div() where you could pass the tye arguments more easyly

styled.div<Props>()`styles here`
Read more comments on GitHub >

github_iconTop Results From Across the Web

Defining Props in React Function Component with Typescript
This guide will provide you with the syntax you need to properly define the props entering your components, compare and contrast defining ...
Read more >
Using styled-components with props and TypeScript
You can specify the interface/type of your props to styled-components ... First, create a withProps.ts file with the following content:
Read more >
Best Practices for Using TypeScript and React - OneSignal
The first helper type to use here is React.PropsWithChildren , which automatically adds the children prop to the component:
Read more >
Using styled-components in TypeScript: A tutorial with examples
This installs the styled-components types for TypeScript as a dev dependency ... In the styles folder, create a file and name it global.ts...
Read more >
Working With React Hooks and TypeScript - Toptal
Add TypeScript to the mix, and developers can leverage static typing and ... The function explicitly declares its return type, setting along the...
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