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
[docs] Add paragraph on withStyles with multiple classes #9851
Conversation
**Summary**: Add a paragraph to the TypeScript specific documenation of `withStyles` that explains how to type components receiving multiple classes from `withStyles`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your PR! I can't comment on the technical content, so just some suggestions on style. We're gradually sharpening up the docs to follow technical writing guidelines, but we still have a long way to go!
docs/src/pages/guides/typescript.md
Outdated
@@ -94,6 +94,44 @@ const DecoratedNoProps = decorate<{}>( // <-- note the type argument! | |||
|
|||
To avoid worrying about this edge case it may be a good habit to always provide an explicit type argument to `decorate`. | |||
|
|||
### Injecting Multiple Classes | |||
|
|||
TypeScript does not support variadic types, therefore there is no clean way to type usage of `withStyles` that injects multiple classes into a component. There is a hacky workaround that allows you to get support for classes inside your component while only exposing the props you define. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"However, there is a hacky workaround"
docs/src/pages/guides/typescript.md
Outdated
|
||
TypeScript does not support variadic types, therefore there is no clean way to type usage of `withStyles` that injects multiple classes into a component. There is a hacky workaround that allows you to get support for classes inside your component while only exposing the props you define. | ||
|
||
Take the following code as an example: We define our own props in the `Prop`-type, while the props the component actually receives is a union of our `Prop`-type and a `WithStyles`-type for each respective style we defined (`one` and `two`). To prevent TypeScript from exposing the props set through `withStyles` to the consumers of our component, we cast it to a `React.ComponentType<Props>` before exporting it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Take the following code for example."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, could you perhaps rephrase this without the personal pronoun "we"?
docs/src/pages/guides/typescript.md
Outdated
someProp: string; | ||
}; | ||
|
||
type PropsWithStyles = Props & WithStyles<"one"> & WithStyles<"two">; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was expecting:
type PropsWithStyles = Props & WithStyles<'one' | 'two'>;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow, this is an even better solution. Thanks for pointing this out. I played around a bit and looked at the types, but never thought about using a |
.
I will update tomorrow with @oliviertassinari suggestion and update the text accordingly. Thanks for pointing out this solution. |
docs/src/pages/guides/typescript.md
Outdated
@@ -94,6 +94,44 @@ const DecoratedNoProps = decorate<{}>( // <-- note the type argument! | |||
|
|||
To avoid worrying about this edge case it may be a good habit to always provide an explicit type argument to `decorate`. | |||
|
|||
### Injecting Multiple Classes | |||
|
|||
TypeScript does not support variadic types, therefore there is no clean way to type usage of `withStyles` that injects multiple classes into a component. However, there is a hacky workaround that allows you to get support for classes inside your component while only exposing the props you define. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this paragraph, or to the extent that I do, I don't think it's accurate. It's certainly possible to inject multiple classes into a component.
docs/src/pages/guides/typescript.md
Outdated
@@ -94,6 +94,42 @@ const DecoratedNoProps = decorate<{}>( // <-- note the type argument! | |||
|
|||
To avoid worrying about this edge case it may be a good habit to always provide an explicit type argument to `decorate`. | |||
|
|||
### Injecting Multiple Classes | |||
|
|||
Injecting multiple classes into a component is straightforward possible. Take the following code for example: The classes `one` and `two` are both available with type information on the `classes`-prop passed in by `withStyles`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"is as straightforward as possible" (add 2 x "as")
Regarding "Take the following code for example:" it should be something other than a colon here, as the example doesn't immediately follow, but I can't decide if it should be a full stop (period), or a hyphen, running on to the next clause. Take your pick, either will work.
Sorry for being picky. 😈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem. Just please no more edits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or just edit the PR directly. :D
Sorry for the ninja edit after your approve. There still was a typing error. |
@clentfort NP, I was only approving my requested text changes. I'll let someone who knows TS review the code samples. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My honest feeling is that this section doesn't add much; I'd rather just augment the previous section so that it has more than one class. That said, I don't object to it being merged.
Summary:
Add a paragraph to the TypeScript specific documenation of
withStyles
that explains how to type components receiving multiple classes fromwithStyles
.