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] add sample with return types #11512

Merged
merged 1 commit into from
May 24, 2018
Merged

Conversation

yacut
Copy link
Contributor

@yacut yacut commented May 21, 2018

With typescript 2.8+ ReturnType feature could by used.

@yacut yacut changed the title add sample with return types [typescript] add sample with return types May 21, 2018
@hexetia
Copy link
Contributor

hexetia commented May 22, 2018

this partially work for me, the ReturnType can correctly infer the properties names, but that affect the css type safety '-'

import * as React from "react";
import { Theme, withStyles, WithStyles } from "@material-ui/core/styles";

const styles = (theme: Theme) => ({
	one: {
		backgroundColor: "red",
		textAlign: 'center'
	},
	two: {
		backgroundColor: "pink"
	}
});

interface Props {
	someProp: string;
}

class SomeComponent extends React.Component<Props & WithStyles<keyof ReturnType<typeof styles>>, {}> {
	render() {
		return <span className={this.props.classes.xxx}>maria</span>;
	}
}

export default withStyles(styles)<Props>(SomeComponent);

image

broken css type safety
image

@hexetia
Copy link
Contributor

hexetia commented May 22, 2018

with a little helper function(found in https://stackoverflow.com/a/49539369) the css type safety works in a beautiful way 😍

img

import * as React from "react";
import { Theme, withStyles, WithStyles } from "@material-ui/core/styles";
import {CSSProperties} from "@material-ui/core/styles/withStyles";

function createStyleMap<T extends { [name: string]: CSSProperties }>(cfg: T) : Record<keyof T, CSSProperties> {
	return cfg;
}

const styles = (theme: Theme) => createStyleMap({
	one: {
		backgroundColor: "red",
		textAlign: 'center'
	},
	two: {
		backgroundColor: "pink"
	}
});

interface Props {
	someProp: string;
}

class SomeComponent extends React.Component<Props & WithStyles<keyof ReturnType<typeof styles >>, {}> {
	render() {
		return <span className={this.props.classes.two}>maria</span>;
	}
}

export default withStyles(styles)<Props>(SomeComponent);

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.

This looks good to me. As far as the helper method for defeating TypeScript type widening, I think this is useful enough that we should consider adding it to the core library (even though for JS users it's useless, since it's just the identity function!) But that should probably be a separate PR.

@pelotom
Copy link
Member

pelotom commented May 28, 2018

#11609 adds a styles creation helper similar to that proposed by @saculbr; take a look at the updated TypeScript guide there and feel free to comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants