-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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] Constrain props type param appropriately in withStyles, withTheme, withWidth HOCs #11003
[typescript] Constrain props type param appropriately in withStyles, withTheme, withWidth HOCs #11003
Changes from 13 commits
e1f809d
f488adf
3560ac0
8eb517a
175fa49
92bee1b
c01d787
89d2a60
bcbf701
9fbbbc1
e66b8ea
0b47fb3
748af39
b24250b
ac934c1
f1dbf16
ea20df9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,45 +56,7 @@ const DecoratedClass = decorate( | |
); | ||
``` | ||
|
||
Note that in the class example you didn't need to annotate `<Props>` in the call to `decorate`; type inference took care of everything. However, there are 2 scenarios where you _do_ need to provide an explicit type argument to `decorate`. | ||
|
||
Scenario 1: your styled component takes _no_ additional props in addition to `classes`. The natural thing would be to write: | ||
|
||
```jsx | ||
import { WithStyles } from 'material-ui/styles'; | ||
|
||
const DecoratedNoProps = decorate( | ||
class extends React.Component<WithStyles<'root'>> { | ||
render() { | ||
return ( | ||
<Typography classes={this.props.classes}> | ||
Hello, World! | ||
</Typography> | ||
); | ||
} | ||
} | ||
); | ||
``` | ||
|
||
Unfortunately, TypeScript infers the wrong type in this case and you'll have trouble when you go to make an element of this component. In this case, you'll need to provide an explicit `{}` type argument, like so: | ||
|
||
```jsx | ||
import { WithStyles } from 'material-ui/styles'; | ||
|
||
const DecoratedNoProps = decorate<{}>( // <-- note the type argument! | ||
class extends React.Component<WithStyles<'root'>> { | ||
render() { | ||
return ( | ||
<Typography classes={this.props.classes}> | ||
Hello, World! | ||
</Typography> | ||
); | ||
} | ||
} | ||
); | ||
``` | ||
|
||
Scenario 2: `Props` is a union type. Again, to avoid getting a compiler error, you'll need to provide an explict type argument: | ||
When your `props` are a union, Typescript needs you to explicitly tell it the type, by providing an explicit generic `<Props>` parameter to `decorate`: | ||
|
||
```jsx | ||
import { WithStyles } from 'material-ui/styles'; | ||
|
@@ -125,7 +87,6 @@ const DecoratedUnionProps = decorate<Props>( // <-- without the type argument, w | |
); | ||
``` | ||
|
||
To avoid worrying about these 2 edge cases, it may be a good habit to always provide an explicit type argument to `decorate`. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussion of the second (now only) scenario needs to be restored here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think not, if the compiler version can be moved forward. See below. |
||
### Injecting Multiple Classes | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import * as React from 'react'; | ||
import { WithTheme } from '../styles/withTheme'; | ||
import { Omit, ConsistentWith } from '..'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
import { Theme } from './createMuiTheme'; | ||
|
||
/** | ||
|
@@ -28,9 +30,8 @@ export interface WithStylesOptions { | |
|
||
export type ClassNameMap<ClassKey extends string = string> = Record<ClassKey, string>; | ||
|
||
export interface WithStyles<ClassKey extends string = string> { | ||
export interface WithStyles<ClassKey extends string = string> extends Partial<WithTheme> { | ||
classes: ClassNameMap<ClassKey>; | ||
theme?: Theme; | ||
} | ||
|
||
export interface StyledComponentProps<ClassKey extends string = string> { | ||
|
@@ -41,6 +42,11 @@ export interface StyledComponentProps<ClassKey extends string = string> { | |
export default function withStyles<ClassKey extends string>( | ||
style: StyleRules<ClassKey> | StyleRulesCallback<ClassKey>, | ||
options?: WithStylesOptions, | ||
): <P>( | ||
component: React.ComponentType<P & WithStyles<ClassKey>>, | ||
) => React.ComponentType<P & StyledComponentProps<ClassKey>>; | ||
): { | ||
( | ||
component: React.ComponentType<WithStyles<ClassKey>>, | ||
): React.ComponentType<StyledComponentProps<ClassKey>>; | ||
<P extends ConsistentWith<WithStyles<ClassKey>>>( | ||
component: React.ComponentType<P & WithStyles<ClassKey>>, | ||
): React.ComponentType<P & StyledComponentProps<ClassKey>>; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about making a reusable version of this, type ConsistentWith<O> = Partial<O> & Record<string, any>; which can then be used for P extends ConsistentWith<WithStyles<ClassKey>>
P extends ConsistentWith<WithWidthProps>
P extends ConsistentWith<WithTheme> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semicolon |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import * as React from 'react'; | ||
import { Omit } from '../../src'; | ||
import Typography, { TypographyProps } from '../../src/Typography/Typography'; | ||
import { withStyles, WithStyles } from '../../src/styles'; | ||
|
||
|
@@ -35,7 +36,7 @@ const DecoratedClass = decorate( | |
}, | ||
); | ||
|
||
const DecoratedNoProps = decorate<{}>( | ||
const DecoratedNoProps = decorate( | ||
class extends React.Component<WithStyles<'root'>> { | ||
render() { | ||
return <Typography classes={this.props.classes}>Hello, World!</Typography>; | ||
|
@@ -46,3 +47,33 @@ const DecoratedNoProps = decorate<{}>( | |
const sfcElem = <DecoratedSFC text="Hello, World!" variant="title" color="secondary" />; | ||
const classElem = <DecoratedClass text="Hello, World!" variant="title" color="secondary" />; | ||
const noPropsElem = <DecoratedNoProps />; | ||
|
||
// This is the "scenario 2" example straight from the doc, then invoked: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no scenario 2 now, maybe just provide a link or else delete the comment. |
||
|
||
interface Book { | ||
category: 'book'; | ||
author: string; | ||
} | ||
|
||
interface Painting { | ||
category: 'painting'; | ||
artist: string; | ||
} | ||
|
||
type ArtProps = Book | Painting; | ||
|
||
const DecoratedUnionProps = decorate<ArtProps>( // <-- without the type argument, we'd get a compiler error! | ||
class extends React.Component<ArtProps & WithStyles<'root'>> { | ||
render() { | ||
const props = this.props; | ||
return ( | ||
<Typography classes={props.classes}> | ||
{props.category === 'book' ? props.author : props.artist} | ||
</Typography> | ||
); | ||
} | ||
}, | ||
); | ||
|
||
const unionPropElem = <DecoratedUnionProps category="book" author="Twain, Mark" />; | ||
|
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.
Remove the "explicit", it's implicit that it's explicit from the preceding "explicitly" :)