-
-
Notifications
You must be signed in to change notification settings - Fork 401
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: resovle createUseStyle generics order #1469
Conversation
@kof Please see the entire discussion that happened from this comment onward in the linked PR. If you decide you want to go with these changes, that's fine. But from what I understand, I strongly recommend leaving the code as is. |
That hard to define props, we don't have to define classes names. |
Lets discuss this here, because the linked discussion is on a closed PR and this one seems like a good place to figure it out: Do I understand correctly that you see different compiler errors from the same types? If yes what can be the reason, different TS versions? |
The complaint doesn't seem to be about getting compiler errors for the same types. The complaint seems to be that hosseinmd doesn't like having to specify |
@hosseinmd do you say the RuleNames type is inferred correctly for you and allows you to get the errors when using classes without manually specifying the RuleNames? |
I am actually wondering if it's possible to not have to specify RuleNames, because technically they are already there in the object and TS could use those keys and map them to classes |
@kof Unfortunately, that's not possible. I found this out the hard way while working on #1460, and k-yle seems to understand this problem as well (see #1453). I'll try to go through the logic of why... but it's a lot of logic. Note that I'm going to try to condense this logic, and there is much more that can be said about why we're stuck in the situation of needing to define We Need
|
I should note that once microsoft/TypeScript#26349 gets merged, I'm hoping we can get away with something like this: declare function createUseStyles<Props = unknown, Theme = DefaultTheme>(
styles: Styles<_, Props, Theme> | ((theme: Theme) => Styles<_, Props, undefined>),
options?: CreateUseStylesOptions<Theme>
): (data?: Props & {theme?: Theme}) => ClassesForStyles<typeof styles> But that's mildly hackish and it's not readily apparent if that would be "legal" once the new Microsoft changes get merged. Additionally, if we applied this change, users would never be able to specify strict rules on
Correction: Using the |
Adding to @ITenthusiasm's explanation: here's an example in the typescript playground that shows that the order of the generics makes no difference |
To understand the problem I am trying to reproduce it on a simple playground, here is what I got so far, but I am guessing it's not representing the actual problem. @ITenthusiasm what do I need to add there to make it show the problem? this will be maybe a good example to explain for future readers https://www.typescriptlang.org/play?jsx=0&ts=4.2.3#code/MYewdgzgLgBFIGEA2BDCECmEYF4YB4BlKATySwD4AKaMrALhgG8YBtAJQFdyA5FAWwwwAlmBgBrDCRAAzGMToQAuo2gAnUQHMYAXwCUuCjFrkIAbgCwAKGuhIsGWpD8FpgCqJU6LLgKvKNKSmjP4QBjhGTNYwMHbQsV6YEIwoYCS+TDrRMDIgajBUcbCS6aLGQVgGUVYxMcCJWKwlSr4A5PVoEHyCrdlZNTAA9IMwbhqamhj5ojJTGGDAGNlqGFCcamLwyJ1YRBUQ1B3eYdb9tuDxR0m+js6hHtvHVNUxKIwARACM7wA02QBGjFaACZelZ9NZbA0IAA6YBmGBAA |
Yes, it is possible, if we move ruleNames to last generic param, so we just need to define props and ruleNames will be recognized |
What version of TS are you all testing with? Just to make sure this is not the source of our differences |
I testing now, give me time. |
@ITenthusiasm is right, RuleNames not inferred The benefit of this PR is //change this line
const themeArg3 = createUseStyles<string, MyProps>
// to this line
const themeArg3 = createUseStyles<MyProps> |
Sorry this PR is wrong. |
@kof Yeah that's not quite the same as the actual problem. There are a few reasons why, but I don't think diving into those reasons would advance the React-JSS discussion much more, and I don't want to write another chunk of text. @k-yle added a really good TypeScript playground to reflect the issue (thanks). I don't think a more relevant example can be added than his. If someone wants to see a basic example of the issue I was describing, microsoft/TypeScript#43119 is a sufficient example. If someone wants to see an example of this issue in the context of React-JSS, then k-yle's TS playground is a sufficient example.
If we're talking about documenting things for future readers, then honestly, my first comment on the matter and k-yle's are enough. From this discussion and PR alone, there clearly isn't a "simple" or "easy" way to explain the problem in 2 sentences. And a random isolated example isn't going to make sense without the background explanation. What's also clear is that many people will likely be unwilling to read something as large as a condensed explanation anyway, especially when they're running with (understandable) presumptions about how TS should operate. And if that's the case, no amount of talking or isolated examples will help them. People will just have to run into the problem themselves and bang their heads against the wall like I had to do. They'll learn the truth one way or another by playing with the source code or k-yle's playground. I can't help anyone further than what I've said. And at this point I've given the same explanations and set of references at least 2-3 times between this PR and #1460. |
@ITenthusiasm yeah, totally understandable, it must be frustrating, consider this as teaching us I haven't used typescript yet (at the job still flowtype), will soon start using it eventually though. I played a bit more and found this workaround as alternative to typing out the list of rule names: the idea here is to use typeof styles at the call site so that consumer doesn't have to define rule names list |
closing this PR as we figured this out |
Holy s. this thing falls apart as soon as I try declaring styles as a separate object, Props doesn't get used any more for function values |
Corresponding issue (if exists):
#1460
Currently, for add props types we have to add classes name first.
What would you like to add/fix?
I changed order of generic types of createUseStyle
Now we don't have to add classes name, because classes names recognizing from inserted object.
Todo
Changelog
Please summarize the changes in a way that makes sense inside the changelog. Feel free to add migration tips or examples if necessary.