You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How do I provide props to styled elements in Typescript? #630
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 I don't know how to make it not error.
@tal Assuming that TypeScript infers the generics correctly — Disclaimer 😉 I haven't used SC with TypeScript yet — you'll just need to type your interpolation argument:
Reacted by Taras Vozniuk, Pierre Gee, JimmyLv_吕立青, jimmy-guo, Kana Abe, Lukas Guschlbauer, 西风逍遥游, Searene, rr-kalbeck, Bill Miller and 130 more
Reacted by Caique M. Oliveira, Nino, Spencer Gardner, Yarden Hochman, JsonWrl, Piotr, Celo Reis, Arryangga Aliev Pratamaputra and IGOR REBECHE ROSA
Reacted by Steve Boyer, Bahaa Jr., Joey Byeonggyu Choi, Sávio Serra, Ivo, veganshrimp, G. Herzog, Gustavo Kich, Nicklas 'Nick' Reincke, Felipe Augusto and 10 more
On Thu, Mar 30, 2017 at 7:04 PM Phil Plückthun ***@***.***> wrote:
@tal <https://github.com/tal> Assuming that TypeScript infers the
generics correctly — Disclaimer 😉 I haven't used SC with TypeScript yet
— you'll just need to type your interpolation argument:
const Input = styled.input` border: ${(p: YourProps) => p.invalid ? 'red' : 'blue'};`
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#630 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABJ2rxp8PI2i2qOGk_Ym6ZHqA4Lz4Xfks5rrDUEgaJpZM4Mu9JF>
.
Reacted by Qwerty (Vítězslav Ackermann Ferko), Eric Burel, Laxmikant Dange, Zach Breit and Rohyl Dwaine
EDIT: Holdup. What I had just happened to fit my usecase.
This currently seems to be the best approach, albeit a bit wordy.
Reacted by Justus Romijn
Reacted by Per Sandstrom, Steve Barman, Sahal Sajjad, Mateusz Leonowicz, lilairpod, Chris Geirman, Kaelan Cooter, Paul, Michał Szydłowski, Lexi Aiello and 4 more
Reacted by Gijsjan Brouwer, Taras Vozniuk, Chris, Searene, Konrad, Sergey Filkin, Mo Rajabi, Daniel, Justin Young, kudohamu and 141 more
Reacted by sisi, Shripad Krishna, Anandaroop Roy, Linus Svahn, Erifranck Nuñez, Zheng Wang, Kyle Kelley, Kaelan Cooter, Patrick Campbell and Victor
Reacted by Atif Afzal, Taras Vozniuk, Mo Rajabi, sisi, Tomasz Glazer, Shripad Krishna, Jesse Pinho, Christos Pappas, Changsheng Wu, vi and 8 more
Reacted by Marcus Vinicius Carvalho, Carlo Palinckx, Lalli Nuorteva, Rémi Chanaud, Daniel Del Core, maierson, Gerard O'Neill, Roman Khitin, Gabriel Bull, Yann RENAUDIN and 17 more
Reacted by sisi, Jason Watson, Nate, Shripad Krishna, Christos Pappas, Changsheng Wu, Hayato Mizuno, thrave, Xiaoheng Liu, Linus Svahn and 18 more
Reacted by Kirill Alexander Khalitov, Brett, alid-code-archives, eric yang, Alex Akagi, Xiaoheng Liu, Emanuel Quimper, Ville Venäläinen, Mateusz Leonowicz, AwesomeObserver and 6 more
Reacted by pravdomil, Peter Kieltyka, Łukasz Wiatrak, user3587412, Peter Weller, Daniel K., Vitaly, Jayson Cleary, Gennadii Fairushin, Mateusz Leonowicz and 12 more
Reacted by Fabian Simon
Reacted by Atif Afzal, Aris Plakias, Daniel K., Vitaly, Hayato Mizuno and Mateusz Leonowicz
@Igorbek Have you noticed that vscode's typescript styled plugin seems to not work when using any of these syntaxes?
Edit:
In the mean time I've aliased styled to themed and then add props to each of the types I want to use. Could probably be optimized more to be less ugly:
Reacted by Andrew Yasso, Volodymyr V., sundaycrafts, Vitalii Hopaniuk, Christopher Katsaras, kjnnklllk, clodal, Robert Molina, Matthias Christoph Munder, Jørgen Tvedt and 5 more
Reacted by Paul Melnikow, Robert Molina and Sergey S. Volkov
Reacted by Volodymyr V., Robert Molina and Christopher Train
Reacted by Ivan Kleshnin, Robert Molina and Sergey S. Volkov
Reacted by Volodymyr V., Robert Molina, Matthias Christoph Munder and Christopher Train
@ni3t Is that a recent thing? When I try that with an interface type I get:
ERROR in [at-loader] ./src/components/InverterAnalysis.tsx:153:32
TS2344: Type 'InverterAnalysisProps' does not satisfy the constraint '"symbol" | "object" | "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | "b" | "base" | "bdi" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | ... 155 more ... | ComponentClass<...>'.
Type 'InverterAnalysisProps' is not assignable to type 'ComponentClass<any, any>'.
Type 'InverterAnalysisProps' provides no match for the signature 'new (props: any, context?: any): Component<any, any, any>'.
Reacted by Ivan Kleshnin, Anri Asaturov, Jimmy Multani and Christoph Michel
@CMCDragonkai I think you tried something like styled<MyExtraProps>.button to get that error. You need to use styled.button<MyExtraProps>.
To summarize:
// Some properties that we use in our styles, // but are not part of the underlying component's props.typeMyExtraProps={anExtraProp: WhateverType};// For basic HTML elements:constMyStyledComponent=styled.button<MyExtraProps>` ... ${props=>props.anExtraProp} ...`;// For extending components that support the className prop:constMyStyledComponent=styled<MyExtraProps>(AStyleableComponent)` ... ${props=>props.anExtraProp} ...`;
(P.S. The versions in my package.json are styled-components@^4.2.0, @types/styled-components@^4.1.16, and typescript@^3.3.4000.)
Reacted by Paul Melnikow, Jorge Padilla, clodal, Opharion, Patrick Dahms, Paweł Jędrzejczyk, Ray Epps, Alex Feinman, Jarek P. and Ezell
Based on the above comment and my experience using styled-components with React Native, it seems that styled<SomeInterface>(Text) in @ni3t's answer should be styled.Text<SomeInterface>.
Reacted by Wataru Ikeda, Opharion, Maks Kolodiy, Kurt, Weerapat Poosri and Christopher Train
Reacted by Jake Lacey, Gustav Bylund, Igor Samulenko, Travis Scott, RL, KIKUCHI Yuki, Will Hart, Gabriel Russ, Yaroslav, Josnei Luis Olszewski Junior and 28 more
Reacted by Jake Lacey, williamsk91, Aleš Vojta, gunar and erukiti
Reacted by AJ Foster, RL, JP Erasmus, gunar, Felipe César, erukiti and Ronny Ewanek
Reacted by JP Erasmus, gunar, erukiti and DalerLeo
Activity
kitten commentedon Mar 30, 2017
@tal Assuming that TypeScript infers the generics correctly — Disclaimer 😉 I haven't used SC with TypeScript yet — you'll just need to type your interpolation argument:
tal commentedon Mar 30, 2017
mxstbr commentedon Apr 1, 2017
/cc @styled-components/typers any ideas?
jupl commentedon Apr 2, 2017
EDIT: Holdup. What I had just happened to fit my usecase.
This currently seems to be the best approach, albeit a bit wordy.
giladaya commentedon Apr 5, 2017
@philpl 's approach worked for me
beshanoe commentedon Jun 7, 2017
I'm using v2 and found this way more convinient for plain containers:
patrick91 commentedon Jun 7, 2017
@beshanoe hopefully we'll have a way to do this in TS (see: microsoft/TypeScript#11947)
mxstbr commentedon Jun 30, 2017
Going to close this as there isn't much we can do right now about this, thanks for discussing!
alloy commentedon Jul 14, 2017
This is what I use at the moment:
atfzl commentedon Jul 22, 2017
Extending @David-Mk's method to get theme types as shown in https://www.styled-components.com/docs/api#typescript
Igorbek commentedon Jul 23, 2017
Another option which allows to bypass explicit element type specification, but looking a bit awkward:
so later:
jimthedev commentedon Oct 18, 2017
@Igorbek Have you noticed that vscode's typescript styled plugin seems to not work when using any of these syntaxes?
Edit:
In the mean time I've aliased
styledtothemedand then add props to each of the types I want to use. Could probably be optimized more to be less ugly:Edit: Opened issue here: microsoft/typescript-styled-plugin#21
75 remaining items
ni3t commentedon Jun 6, 2019
The
styledwrapper can be passed in a type like so:CMCDragonkai commentedon Jun 18, 2019
@ni3t Is that a recent thing? When I try that with an interface type I get:
yasso1am commentedon Jun 18, 2019
Can confirm that @ni3t 's recommendation worked just fine for me.
hypeofpipe commentedon Jun 28, 2019
Thanks @ni3t 🎈
joshuatshaffer commentedon Jul 5, 2019
Thanks @ni3t. That helped a lot.
@CMCDragonkai I think you tried something like
styled<MyExtraProps>.buttonto get that error. You need to usestyled.button<MyExtraProps>.To summarize:
(P.S. The versions in my
package.jsonarestyled-components@^4.2.0,@types/styled-components@^4.1.16, andtypescript@^3.3.4000.)paulmelnikow commentedon Jul 11, 2019
Based on the above comment and my experience using styled-components with React Native, it seems that
styled<SomeInterface>(Text)in @ni3t's answer should bestyled.Text<SomeInterface>.ni3t commentedon Jul 25, 2019
Ah I've never tried it using the built-in
styled.Whatever.Thanks for the feedback!
openkalki commentedon Aug 28, 2019
I came across this issue today and found something that worked for my use-case.
I wanted to:
Here is how I solved it:
Hope this helps people
noelrappin commentedon Aug 30, 2019
So, I had this:
Which was fine, but when I tried to add a prop type:
Then I get a TypeScript error, because it doesn't recognize
attrsanymore.Is there a workaround here I'm missing?
joshuatshaffer commentedon Aug 31, 2019
@noelrappin Try this:
What's happening is that the
<IFooProps>is you passing a type argument to a generic function.See https://www.typescriptlang.org/docs/handbook/generics.html
glenwinters commentedon Oct 29, 2019
For those that find this issue via google, here's the answer in the documentation:
https://www.styled-components.com/docs/api#using-custom-props
Example:
mayowDev commentedon Nov 8, 2019
i was solving button styled component and this one worked for me
import styled, { StyledComponent } from "styled-components"
const ButtonContainer: StyledComponent<"button", any, {}, never>= styled.button
your code hereKelbie commentedon Feb 13, 2020
paulmelnikow commentedon Feb 13, 2020