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

[typescript] Constrain props type param appropriately in withStyles, withTheme, withWidth HOCs #11003

Merged
merged 17 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 1 addition & 40 deletions docs/src/pages/guides/typescript/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 a generic `<Props>` parameter to `decorate`:

```jsx
import { WithStyles } from 'material-ui/styles';
Expand Down Expand Up @@ -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`.

### Injecting Multiple Classes

Expand Down
3 changes: 3 additions & 0 deletions packages/material-ui/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type Diff<T extends string, U extends string> = ({ [P in T]: P } &
/** @internal */
export type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;

/** @internal */
export type ConsistentWith<O> = Partial<O> & Record<string, any>;

export namespace PropTypes {
type Alignment = 'inherit' | 'left' | 'center' | 'right' | 'justify';
type Color = 'inherit' | 'primary' | 'secondary' | 'default';
Expand Down
16 changes: 11 additions & 5 deletions packages/material-ui/src/styles/withStyles.d.ts
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 { ConsistentWith } from '..';
import { Theme } from './createMuiTheme';
import * as CSS from 'csstype';

Expand Down Expand Up @@ -34,9 +36,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> {
Expand All @@ -47,6 +48,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>>;
};
3 changes: 2 additions & 1 deletion packages/material-ui/src/styles/withTheme.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Theme } from './createMuiTheme';
import { ConsistentWith } from '..';

export interface WithTheme {
theme: Theme;
}

declare const withTheme: () => <P>(
declare const withTheme: () => <P extends ConsistentWith<WithTheme>>(
component: React.ComponentType<P & WithTheme>,
) => React.ComponentClass<P>;

Expand Down
3 changes: 2 additions & 1 deletion packages/material-ui/src/utils/withWidth.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Breakpoint } from '../styles/createBreakpoints';
import { ConsistentWith } from '..';

export interface WithWidthOptions {
resizeInterval: number;
Expand All @@ -22,6 +23,6 @@ export function isWidthUp(

export default function withWidth(
options?: WithWidthOptions,
): <P>(
): <P extends ConsistentWith<WithWidthProps>>(
component: React.ComponentType<P & WithWidthProps>,
) => React.ComponentClass<P & Partial<WithWidthProps>>;
7 changes: 7 additions & 0 deletions packages/material-ui/src/utils/withWidth.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ export class Hello extends React.Component<IHelloProps & WithWidthProps & WithSt
const Decorated = withWidth()(withStyles(styles)(Hello));

<Decorated name="Bob" />;

const WidthSFC = withWidth()<{
// shouldn't need to specify width here; it's a given
name: string;
}>(({ width, name }) => <div style={{ width }}>hello, {name}</div>);

<WidthSFC name="Hortense" />;
2 changes: 1 addition & 1 deletion packages/material-ui/test/typescript/components.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ const TableTest = () => {
);
}

return withStyles(styles)<{}>(BasicTable);
return withStyles(styles)(BasicTable);
};

const TabsTest = () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/material-ui/test/typescript/styles.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const ComponentWithChildren: React.SFC<WithStyles<ComponentClassNames>> = ({
children,
}) => <div className={classes.root}>{children}</div>;

const StyledExampleThree = withStyles(styleRule)<{}>(ComponentWithChildren);
const StyledExampleThree = withStyles(styleRule)(ComponentWithChildren);
<StyledExampleThree />;

// Also works with a plain object
Expand Down
30 changes: 29 additions & 1 deletion packages/material-ui/test/typescript/styling-comparison.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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>;
Expand All @@ -46,3 +46,31 @@ 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 />;

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" />;