-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[typescript] withStyles()
complains unless using as
#10995
Comments
This is a result of the recent updates to They (the folks working on the jss type defs) are doing some very radical changes without a good rollout process, imho. It's not clear to me what a better process would be, though; maybe someone else has a thought. (I'm not a mui project member or anything, I'm just sharing what I know/think.) |
@estaub Thanks for your reply! I've opened an issue there - DefinitelyTyped/DefinitelyTyped#24952 |
These issues have existed for a long time; the core issue is TypeScript’s type widening. They are just more prominent now that JSS has better typings. See #8928 for techniques to guide the type system. @estaub I’m one of the people pushing the radical changes. What would you suggest as a better rollout process? |
@pelotom |
Another anecdote re: breaking changes: A few months ago, JSS typings didn't exist. There were a few rumblings on the issue tracker, but no one had taken the onus to write them (and someone was squatting on So, I spent an afternoon: threw together some basic types and got them merged into DT. The only interesting thing about them was the use of Apparently, they're massively popular. That popularity also brought folks like @pelotom who has taken the time to make them super precise. So, there's no shadowy cabal of folks trying to "move fast and break [typings]." We don't even know each other. We have just independently tapped into an unmet need of providing typings for this library. I understand that changes can be painful. Part of the reason there's no roadmap is because there's no team to coordinate. The changes come from anyone who happened to feel like making them and opening a PR. |
@pelotom I agree that type widening sucks in many scenarios. However, I think there's a way to disable it, but that requires some type definition changes. See this - https://blog.mariusschulz.com/2017/02/04/typescript-2-1-literal-type-widening#non-widening-literal-types |
@franklixuefei I don't see anything in that link that suggests a way to change the type definitions to solve this problem. But if you have specific suggestions I'm all ears! I made a proposal a while back to solve this problem, and others have been made as well. I don't know what to suggest except comment on those issues, or create new proposals. |
@pelotom In his post, he mentioned that
I guess in order to achieve this, And I just went over and read about your post under Typescript, which is great! I hope this can raise their attention a bit. We really need a flag in the compiler options to turn it off. |
I think you may be getting confused between interfaces and object literals. |
For other possibilities and a workaround, see DefinitelyTyped/DefinitelyTyped#24952 (comment) |
withStyles()
complains unless using as
withStyles()
complains unless using as
I avoid this error by declaring type on the argument being passed to import { StyleRulesCallback, withStyles } from 'material-ui/styles';
const styles: StyleRulesCallback<'root'> = () => ({
root: {
position: 'relative',
},
});
const decorate = withStyles(styles); |
@varoot if you have a lot of them, you may find this less tedious: Somewhere, once: export function makeTypeCheckProperties<MemberType>():
<S extends {[key in K]:MemberType}, K extends keyof S>(ss: S) => S {
return s=>s
}
export const checkSheet = makeTypeCheckProperties<CSSProperties>() Then I follow a rule of thumb that may or may not be worth following for you:
This helps with some other scenarios, too, like using const styles = () => (checkSheet({
root: {
position: 'relative',
},
}));
const decorate = withStyles(styles); I'd be grateful for feedback from anyone who tries this. |
There was a proposal once upon a time to add something like this (albeit at a more granular level) to the library: But it was shut down. Maybe worth reviving? |
@pelotom I actually started to write a PR a few days ago, but got PR fatigue ;-). If you want to run with it, please do! Otherwise, I'll be back with it in a few days/weeks. The |
@appsforartists as far as I know. It was the other way around. @pelotom pushed for adding the projet as a dependency of Material-UI. |
This should be resolved by #11609, take a look at the updated TypeScript guide there and feel free to comment. |
@pelotom can't wait for these changes to land in a release! Thanks! |
Expected Behavior
No type errors should be reported for adding legal styles kv pairs in
withStyles()
Current Behavior
For some
style
properties, likeposition
,overflow
, etc, specifying a valid value, e.g.,position: 'absolute'
oroverflow: 'auto'
reports type errors.Example:
The only hack is to use
as
:Your Environment
The text was updated successfully, but these errors were encountered: