-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add types to react-emotion #398
Conversation
@@ -0,0 +1,14 @@ | |||
{ | |||
"compilerOptions": { | |||
"target": "es5", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For glamorous we've added declaration: true to help make sure we're exporting everything required for use in libraries microsoft/TypeScript#5938.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! Added.
No warnings
😄
Awesome work doing these types. They're looking great 👍 . |
|
||
export type Interpolation<Props = {}> = | ||
| InterpolationFn<Props> | ||
| EmotionInterpolation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know much about typescript but I don't think this handles the fact that there can be nested function interpolations and they will receive props.
const SomeComponent = styled.div`
display: ${(p) => props => props.display};
color: ${[props => 'hotpink']};
`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works on emotion?
props => props_ => ...
It doesn't make any sense to me. What's this use case for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know exactly all edge cases of emotion API. I'll try to study it to improve the coverage support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mitchellhamilton I've added support to these two cases you mentioned.
export type InterpolationFn<Props = {}> =
(props: Props) =>
| EmotionInterpolation
| InterpolationFn<Props>
export type InterpolationTypes<Props = {}> =
| InterpolationFn<Props>
| EmotionInterpolation
export type Interpolation<Props = {}> =
| InterpolationTypes<Props>
| InterpolationTypes<Props>[]
If you know more cases, just tell me, please.
But you still need Babel in the build pipeline as I already asked here #163? |
@screendriver This is only typings, do not deal with build, or any emotion code. I don't even know the internals of emotion yet. |
Ah ok. But TypeScript definitions for emotion are a little but useless when you can't use emotion without Babel 😉 (emotion only works as Babel plugin so Babel is mandatory) Apart from that: thank you for the typings 😎👍 |
If you don't want to use babel (or can't) just don't use emotion. Pick another one: styled-components, glamor/ous, fela, etc. Anyway, this is another issue. Don't have any relation with typescript or this pull request. |
Someone can help me with documentation? I think it's the only thing missing to this pr. My suggestion is to write a section "TypeScript" in emotion website, but you are creating a new website, right? @tkh44 |
@renatorib any new markdown file in the |
That's great! I'll try to write something. :) |
This is awesome, I just started using typescript as a replacement for PropTypes, and this will go nicely with that 👌 Thanks @renatorib |
I am not complaining about that. It is much more a question how to use emotion with TypeScript 😉 Because you found obviously a way that I am not aware of at the moment and I really want to use emotion as well. Great work 👍 |
For now I'm 'blocked' from using emotion in my company codebase because we use TypeScript and emotion don't have typings. If you really needs use emotion and can't use Babel, you can:
You also can use Babel and TypeScript together, I guess. Btw, And thanks for the compliment. |
@renatorib Is this ready except for documentation? |
@tkh44 yep! I did not have free time to write it yet, but it's the only thing missing. |
Btw, If you want, you can merge it and I send another PR with documentation. |
I've added a typescript documentation Unfortunately my english is not as good as I wish it were. 😢 Thanks! |
Great work @renatorib! I did a quick edit but I'd like someone who knows more about typescript do a review. |
Looks great @tkh44, thanks! |
What:
TypeScript declarations for react-emotion package
Why:
Support people who use TypeScript and need typings.
How:
react-emotion.d.ts
and linked in "types" package.json fieldChecklist:
Supports
Props Inference by Tag Name:
Supports template string and object as styles:
Passing Custom Props:
Note1: needs a second type parameter passing tag name because it stop to infer when you pass props at first parameter
Note2: glamorous do not support custom props with shorthands, like Link2 😛. It was a little tricky, but works...
Create styled with another component:
Pass custom props to style another component:
Note3: as well as Note1 it's not infer 'MyComponent's Props' when you pass props as first type parameter, so you need to pass 'MyComponent' props together
Theming props:
By default props.theme is
any
, but you can create another styled instance passing theme props.Basically the same approach from styled-components
withComponent:
Also works passing another component instead of tag name