[TS] Proposal: add helper method for adding types to props
See original GitHub issueHi 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:
- Created 6 years ago
- Reactions:4
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The alternative is defining the use a string inside of
styled()
such asstyled('div')
. We can augment the style definitions like so:Then we can call:
@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 likestyled.div()
where you could pass the tye arguments more easyly