-
-
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
Want TypeScript error for when css prop is passed a function (instead of SerializedStyles) #1447
Comments
That's because functions are supported as values of css prop, a function in that position gets called with theme as argument. |
Ah thanks, I see that now. It would be awesome if I had the ability to specify the generic type. Right now, it’s hard coded to any. If I’m reading correctly, setting it to the type of my theme object would be ideal. That way functions that take the theme would be accepted, but functions for dynamic styles that accept some props would be rejected. |
@WestonThayer having a look at this, I can't think of a way to solve this off the top of my head. Because it's an ambient type once the emotion library declares it I'm not sure how you would override them unfortunately. |
After some thinking right now - if we would have to support declaration merging for DefaultTheme then this could have been "fixed". |
I like the declaration merging solution. I just tested it out in an emotion fork. If export interface DefaultTheme {}
...
declare module 'react' {
interface DOMAttributes<T> {
css?: InterpolationWithTheme<DefaultTheme>
}
}
declare global {
namespace JSX {
interface IntrinsicAttributes {
css?: InterpolationWithTheme<DefaultTheme>
}
}
} Then out of the box, I get a really nice TS error when attempting to pass a dynamic style to
And I can easily override the DefaultTheme with:
|
I think you make good points in #1507 (comment) though. Maybe the answer is that I should be using Or maybe this should be solved via an ESLint rule rather than via TypeScript. |
With vanilla |
A manual backwards compat step would be to add instructions to do this:
|
FWIW this is fixed in Emotion 11. |
FWIW on a monorepo setup with several parties using emotion (in my case, chakra, storybook and my own stuff) I had to add a resolution to
|
The problem
When using Emotion, I mostly create "static" styles in variables for use in my JSX to avoid cluttering the JSX with style information. For example:
Occasionally I need to create "dynamic" styles, based off of the component's props:
A common mistake I make is forgetting to call
style()
as a function, and instead writingcss={style}
, which of course doesn't apply any styles because thestyle()
function is never called.I was hoping that using TypeScript with Emotion would help me avoid this error, but Emotion's typings for the
css
prop seem to allow the function. I wish the typings would warn me.Here's an example: https://codesandbox.io/s/mystifying-keldysh-bdker
Proposed solution
Emotion's type definitions should cause a TypeScript error when I pass a function to the
css
prop.The text was updated successfully, but these errors were encountered: