-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[system] style option "transform" has invalid typing #16379
Comments
Could you prepare a codesandbox to play around with? This would help alot in verifying that this is valid at runtime. |
Yes here we go :) https://codesandbox.io/s/busy-joliot-qfkmh Also note, the way the cssValue in the transform function is specified does not allow for better typing when passing the transform function as Being able to spec the cssValue type is important, because it is inferred for the prop value on the enhanced component! :) Maybe it is also possible to allow the object return from transform when cssProperty is false? Probably would require union and not sure if that hits some of the limitations typescript currently has in this regards. |
@eps1lon I found additional errors in the same typing, so related to this issue. I have changed the transform in the following examples to make this other issues with the typing visible. Example: https://codesandbox.io/s/nifty-varahamihira-o13xh Example: https://codesandbox.io/s/boring-hawking-qmtj9 I also noticed some weirdness: export interface StyleOptions<PropKey, Theme extends object> {
cssProperty?: PropKey | keyof CSS.Properties | false;
prop: PropKey;
/**
* dot access in `Theme`
*/
themeKey?: string;
transform?: (cssValue: unknown) => number | string;
} The |
The longer I am on the thing, the more I realize its utterly broken. And that not only on the typing side. I am working on a fix for me and found an implementation error in the style function. Given this monkey patch: import * as CSS from 'csstype';
import {style as brokenStyleFunction} from '@material-ui/system';
type FixedStyleOptions<PropKey extends string, PropType> = {
cssProperty?: PropKey | keyof CSS.Properties | false,
prop: PropKey,
themeKey?: string,
transform?: (cssValue: PropType) => number | string | {[K: string]: any},
}
type FixedStyleFunctionFactory =
<PropKey extends string, PropType>(options: FixedStyleOptions<PropKey, PropType>)
=> (props: {[K in PropKey]?: PropType})
=> any;
const style: FixedStyleFunctionFactory = brokenStyleFunction as any;
export default style; Which gave me:
Note: this is not complete, I am about to figure out the point of the theme prop added in, because normally theme comes from context of component which is styled and no use-case is documented (or I missed it) where theme is passed as option to style. Hence when used as documented you will have to pass theme to every component or pass theme into your style function when you create it... However, before I could figure it out further, I encoutered a runtime implementation error in the style function. I think JSS styled does not expect a null there. |
@eps1lon or someone feels also responsible for it: Is there anything I can do further to make it easier for you so you can confirm whether it is intended or not intended to work like that, or whether my points are valid or not, so further thoughts about PR can me made? I won't start a PR until I know there is no special Idea behind all or some of the things I see as unexpected behavior. I kind of see that you maybe want some of the stuff as a separate issue, that I found out afterwards, but they seem to be the same issue to me. Any mechanics like tagging as "bug" or "confirmed"? or "wontfix"? Thanks in advance ! :) |
Yeah the best course of action for typescript issues is to actually open a PR with your proposed typings as this will run all the tests. Very often people propose solutions that only helps their problem but introduces unsound types or breaks other typing patterns. Having a PR with all the tests and concrete code patches helps a lot. |
I am working on a PR. I would like to sum up the issues:
**1 Who is doing it wrong, In my opinion, the PR should include all the fixes because every issue makes the other fix kind of pointless I ralized. Do you agree? update added links |
You can put this all into a single PR. It would help if you keep changes as atomic as possible i.e. if you can isolate changes please do so via multiple commits in case it would help break it up into PRs after some discussion. For example it seems like the
The thing about monorepos is to allow exactly that. Don't bother too much about crossing package boundaries. |
Got all the stuff fixed and passing tests (including new tests). Now about to break it up like "add fail test for X" "fix X" as you proposed. Kind of hard if you can not simply reference separate issues ;) |
@eps1lon so the style factory is not tested at all type-wise (index.spec.tsx). The moment I simply introduce a test and call |
Can you open a draft PR so I can review what's not working? I don't know what "does not work" means. |
I was talking about the index.spec.tsx, containing tests for the index.d.ts (mui-system). So what is not working is that for usage with So you see, this simple example: I had to for example here to add theme={{}} to hide away the theme issue. The other issues would require further temporary quirks added, before the final fixes actually fix it and I can revert. Kind of weird... When I continue fixing theme, etc, etc in a single fix, all issues are fixed until no such quirks are required. xD |
The typing for the style function only allows transform to number or string. However also objects should be allowed, as you can see in system's package own code here
Expected Behavior 🤔
Should not fail type check:
Current Behavior 😯
Fails with:
Steps to Reproduce 🕹
See expected behavior example.
update Example: https://codesandbox.io/s/busy-joliot-qfkmh
Context 🔦
I think self-explaining by expected behavior.
Your Environment 🌎
The text was updated successfully, but these errors were encountered: