-
-
Notifications
You must be signed in to change notification settings - Fork 397
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
ReactJSS - Incompatible types error (CSS properties for alignment) #1258
Comments
I assume this is a typescript issue. |
I'm also experiencing similar issues with the new typings 10.0.3. In the last few days I've conducted several small experiments in order to track down what works and what doesn't. Some of the insights might be helpful. These errors seem to happen when type inference of TypeScript somehow fails. It only happens for CSS properties where values are typed as union types of different string values. Like I managed to get rid of them when explicitly typing the styles using the It works when using |
This seems to be a common issue, Material UI also has that issue. We could widen all of the |
@HenriBeck Thanks for the clarification! I think it's great to have these strongly typed CSS objects. So widening these properties is not necessary. It's just important to use them in the intended way. That's why I think what's currently missing is a page in the docs that illustrates how to correctly use TypeScript with I can offer to create a PR if this helps. I would just need some help on how to contribute and on how the types should actually be used. For example for the dynamic (themed) use case, I'm still trying to figure out if this would be the correct setup: https://codesandbox.io/s/boring-fermi-qeicu I think I figured out from other issues that it's possible to extract the classNames, even for this dynamic use case: https://codesandbox.io/s/interesting-microservice-z03ny which was an interesting learning for me. Sadly there's one case where this doesn't work: when you create keys for the styles object on the fly. For example we've got helper functions to generate media queries like this: https://codesandbox.io/s/modest-monad-h9jvt In any case: thanks for creating this library and for pushing things forward. :-) |
Regarding the second code sandbox, I will have a look at why the inferring of keys doesn't work there. The last codesandbox can be fixed by explicitly typing the Feel free to create a PR regarding the DOCS or I can make some time next weekend for this |
@HenriBeck I did some additional experiments and thought I'll share my insights. I think type extraction (using the keyof ReturnType pattern) for styles created by a function only seems to work when the So that currently leaves us with two ways to type dynamic styles. Variant A: Explicitly typed literal types and extracted class names import React, { FunctionComponent } from 'react';
import { render } from 'react-dom';
import { createUseStyles, Styles, ThemeProvider } from 'react-jss';
class Theme {
public colorForeground: string;
public colorBackground: string;
constructor() {
this.colorBackground = 'red';
this.colorForeground = 'white';
}
}
const getStyles = (theme: Theme) => ({
'@global': {
'*': {
padding: 0,
margin: 0
}
},
MyComponent: {
color: theme.colorForeground,
background: theme.colorBackground,
textAlign: 'left' as 'left'
}
});
const useStyles = createUseStyles<keyof ReturnType<typeof getStyles>>(
getStyles
);
const MyComponent: FunctionComponent = () => {
const classes = useStyles();
return (
<div className={classes.MyComponent}>This component is styled.</div>
)
};
const App: FunctionComponent = () => {
const theme = new Theme();
return (
<ThemeProvider theme={ theme}>
<MyComponent />
</ThemeProvider>
)
};
const rootElement = document.getElementById('root');
render(<App />, rootElement); A big downside of this variant is that you need to prevent "Incompatible types" errors for CSS properties typed as literal types on every occurrence since you cannot type the whole return value as Variant B: Explicitly typed return Values and hard coded class names //...
type MyComponentClasses = 'MyComponent';
const getStyles = (theme: Theme): Styles => ({
'@global': {
'*': {
padding: 0,
margin: 0
}
},
MyComponent: {
color: theme.colorForeground,
background: theme.colorBackground,
textAlign: 'left'
}
});
const useStyles = createUseStyles<MyComponentClasses>(
getStyles
);
//... In this variant you loose the ability to extract the class names as you explicitly type the return value of the Creating a PR to the docs When looking at the docs website I think a page One last thing You wrote …
But which type should one return there? Sorry for this silly question but I'm still trying to get my head around TypeScript. :-) |
This is related to #1255 |
fixed in 10.0.4 |
Expected behavior:
When a CSS property for alignment is used (such as textAlign and flexDirection), the type definition of the property should match and we should not get an error.
Describe the bug:
When a styles object is passed to createUseStyles, we get the following error in v10.0.3. The error is thrown only when the styles object has a className key that includes a CSS property for alignment, such as -> textAlign: 'right'
Example
SomeComponent:
SomeComponent.styles
Has anyone experienced this and found a way to work around? It works unproblematically when downgraded to v10.0.2
Versions (please complete the following information):
The text was updated successfully, but these errors were encountered: