Skip to content
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

Merged
merged 5 commits into from Jan 13, 2018
Merged

[docs] Add paragraph on withStyles with multiple classes #9851

merged 5 commits into from Jan 13, 2018

Conversation

clentfort
Copy link
Contributor

Summary:

Add a paragraph to the TypeScript specific documenation of withStyles that explains how to type components receiving multiple classes from withStyles.

**Summary**:

Add a paragraph to the TypeScript specific documenation of `withStyles` that explains how to type components receiving multiple classes from `withStyles`.
@oliviertassinari oliviertassinari added the docs Improvements or additions to the documentation label Jan 12, 2018
Copy link
Member

@mbrookes mbrookes left a 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!

@@ -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.
Copy link
Member

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"


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.
Copy link
Member

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."

Copy link
Member

@mbrookes mbrookes Jan 12, 2018

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"?

someProp: string;
};

type PropsWithStyles = Props & WithStyles<"one"> & WithStyles<"two">;
Copy link
Member

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'>;

Copy link
Contributor Author

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 |.

@clentfort
Copy link
Contributor Author

I will update tomorrow with @oliviertassinari suggestion and update the text accordingly. Thanks for pointing out this solution.

@@ -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.
Copy link
Member

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.

@@ -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`.
Copy link
Member

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. 😈

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

@clentfort
Copy link
Contributor Author

Sorry for the ninja edit after your approve. There still was a typing error.

@mbrookes
Copy link
Member

@clentfort NP, I was only approving my requested text changes. I'll let someone who knows TS review the code samples.

Copy link
Member

@pelotom pelotom left a 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.

@oliviertassinari oliviertassinari merged commit 24f5b01 into mui:v1-beta Jan 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants