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 type-defs for styled-components v4 #2933
Add type-defs for styled-components v4 #2933
Conversation
This should hopefully address #2880 |
Also, the justification for the horribly misleading Also, it's added so it works with forward refs (v4 doesn't use |
Damn you, Travis! Doesn't seem to be related to this PR, since |
Travis is back! 🎉 |
I suggest you tag a few people who have worked on this libdef already. :-) |
@AndrewSouthpaw Good idea! Hi @dashed, and @shrynx! Would you two like to take a look at this too, please? 🐈 |
Hi! I'm trying to use it but with no success. Successfully installed as custom definitions but I'm getting errors. How should I use it, @omninonsense? |
Hi @zaguiini! So, the way you're trying to call the function (EDIT: only talking about the type arguments in the call expression) won't work with this libdef (I don't remember if this works for v3). The main reason is that we lie about the type that's returned by What we essentially do is:
This is not ideal, but it means that we have working refs, and prop validation is preserved for the non built-in components. Once forward ref support lands in flow, these can be improved a lot, to even add custom props accepted by the styled component (which the wrapped component ignores, etc), and maybe even properly typing the functions inside the template literals. Although, I think flow doesn't really understand template literals expand to |
@zaguiini I never answered you other question. In the example you provided, I'd just do: type Props = {
a: boolean;
}
const CardContainer = styled.div`
/* ... */
background-color: ${(props: WithTheme<Props>) => props.theme.light.white};
/* ... */
` I usually have a export const theme = {
/* ... */
}
export type Theme = typeof theme
export type WithTheme<P : {} = {}> = P & {| theme: Theme |} |
@omninonsense At the moment, I don't have a project currently using styled-components v4 + flow types; and unfortunately, I won't have the bandwidth to review this PR anytime soon. Awesome job though! 👍 |
you would need to add for macro too |
If we don't get a PR from people with more context within a week, I'm okay with merging it in (after reviewing with little context) and letting to be "better than nothing," and people can continue adding fixes as needed. @omninonsense if this stagnates for too long just ping again and I'll merge it in. |
I have a problem when using the <ListLink as="a" target="_blank" href="https://instagram.com/fintory">
Instagram
</ListLink> But now, I am getting an error, that I am not supplying the right Props:
|
You forgot to add the |
There is indeed one "as" mechanism: https://www.styled-components.com/docs/basics#extending-styles |
I have some updated typings that I need to push, but got busy/distracted by work. I will probably get back to adding some updates early next week after cleaning them up and adding tests to try and add some of the missing APIs (if possible without breaking the typings for the "primary functionality" of the library). |
Added to the checklist! :) |
I decided to give up on I'll look into adding the react native stuff later today or tomorrow. |
I think this is ready-ish for review, @AndrewSouthpaw. At least until we get better support for better typing from @facebook/flow |
For tracking React feature support in flow: |
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.
Generally looks good. Unless there's a strong need, I'd collapse the separate test files into one and scope with describe/it
blocks.
Will do! I only split them up because I assumed it would make them easier to read/understand. 😃 |
@AndrewSouthpaw I used a few (random) other libdefs to see how to structure it. Is it okay like this? |
Had a random epiphany that |
Forgot about Sorry about the noise! |
@egeriis Yes, but see #2933 (comment) about flow not fully supporting template literals. |
@omninonsense Yeah, I read it. Mind slip that I didn't understand it would affect being able to do this. |
I think TypeScript supports something along these lines: const MyDiv: StyledComponentType<'div', Props> = styled.div``; It would be nice if we could support this too someday. |
@omninonsense Idea of effort required to fix the CI errors? |
This allows HOCs can be chained without losing type information
Co-Authored-By: theshortcut <cferris@gmail.com>
I haven't actually looked at fixing it. I rebased and pushed. Hopefully that fixes it. 🤷♂️ (Didn't mean to close, I misclicked, I am stupid) |
@omninonsense It didn't work ;) |
Tests are failing for older versions of the typdef, mostly due to recent Flow changes. |
Which Flow versions is this libdef compatible with? |
Seems reasonable to me! |
Also, would just like to say, way to go @omninonsense on your commitment on this PR. |
I'd like someone to thoroughly review this!
To-do:
styled-components/native
(help wanted)KeyFrames
toInterpolation
CSSRules
(Interpolation[]
) toInterpolation
css
(and maybedkeyframes
?); add comments too.ref
sstyled.derp
/styled('derp')
)attrs
APIThese are currently impossible (to the best of my knowledge) to express these safely and without breaking the types for the "main" functionality of the library, due to Flow's lack of support for some core React features (
React.forwardRef
) this library relies on:withCompnent
APIas
prop API<MySpan myProp=... />
)Please let me know if you can think of anything else to add to the list.
Also, not sure if it's wise to simply merge this (if the PR is good enough), and then hope someone else contributes with whatever I missed?