Skip to content
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

fix(types): add sx prop whenever className can be string #1308

Merged
merged 17 commits into from
Dec 1, 2020

Conversation

hasparus
Copy link
Member

@hasparus hasparus commented Nov 27, 2020

I'm not exactly sure if we have the best possible implementation, as we don't produce className at runtime, we produce css and call Emotion's jsx, so it seems that we should also use their types for correctness.

cc @atanasster @dcastil

Relevant to #1307, but I'm not sure if it closes.


There are three changes here.

  • breaking TypeScript: Renamed and removed types.
    • SxProps to SxProp.
    • SxStyleProp, an alias for ThemeUIStyleObject removed. Use ThemeUIStyleObject instead.
  • Fix: Add sx props types to all props accepting className.
  • Fix WithPoorAsProp to work with ComponentProps utility type.

The change to WithPoorAsProp allows to read props of @theme-ui/mdx Styled on type level. Previously, after passing throught ComponentProps, they became dicts of unknown.

type MyProps = ComponentPropsWithoutRef<typeof Styled.p>

The change to WithConditionalSxProp allows to use sx with all components accepting className of type string, even if they also accept other values for className.

? Omit<P, keyof SxProps> & SxProps
: Omit<P, keyof SxProps>
: Omit<P, keyof SxProps>
export type WithConditionalSxProp<P> = { className: string } extends P
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this one much more than the previous solution. I don't know why Emotion did it that way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, Dany. Now it's even uglier. I forgot that components can have more than one prop while writing this, and that's how I got that nice one-liner :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha 😂

@hasparus
Copy link
Member Author

Okay, I got greedy, and there's going to be another change, what you can probably see in failed tests. I'm making @theme-ui/core and theme-ui strict TypeScript.

@hasparus
Copy link
Member Author

I didn't even touch this 😢
image

LibraryManagedAttributes can literally wreak generic components inference :D Okay, this needs to be fixed on our side. I don't want to add "our jsx pragma is incompatible with react-hook-form" to the readme :D

image

@hasparus
Copy link
Member Author

cc @beerose

Copy link
Collaborator

@beerose beerose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript changes look good to me

@hasparus hasparus merged commit 9c57560 into master Dec 1, 2020
@hasparus hasparus deleted the fix-jsx-types branch December 1, 2020 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants