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] TypeScript example project and guide to withStyles #8694

Merged
merged 1 commit into from
Oct 15, 2017

Conversation

pelotom
Copy link
Member

@pelotom pelotom commented Oct 14, 2017

This includes a port of the create-react-app-with-flow example to TypeScript, as well as a guide to using withStyles. Resolves #8598.

@pelotom pelotom force-pushed the typescript-example branch from 1ef6e1a to 764fccd Compare October 14, 2017 23:49
@pelotom pelotom changed the title Add CRA/TypeScript example project TypeScript example project and guide to withStyles Oct 15, 2017
@pelotom pelotom mentioned this pull request Oct 15, 2017
@oliviertassinari oliviertassinari added docs Improvements or additions to the documentation typescript labels Oct 15, 2017
@oliviertassinari oliviertassinari changed the title TypeScript example project and guide to withStyles [docs] TypeScript example project and guide to withStyles Oct 15, 2017
@oliviertassinari oliviertassinari force-pushed the typescript-example branch 4 times, most recently from 989bfd4 to 6ed02b1 Compare October 15, 2017 16:02
@oliviertassinari oliviertassinari merged commit d7b9992 into mui:v1-beta Oct 15, 2017
@oliviertassinari
Copy link
Member

@pelotom Thanks, this is a great first iteration! Let's see the feedback we can get :).

@pelotom pelotom deleted the typescript-example branch October 15, 2017 21:17
the-noob pushed a commit to the-noob/material-ui that referenced this pull request Oct 17, 2017
@stevegeek
Copy link

@pelotom Thanks for the work on this Guide. Quick question, why when using some JSS properties such as justifyContent I seemingly need to specify the value with a as cast, for example 'center' as 'center' to avoid

          Types of property 'justifyContent' are incompatible.
            Type 'string' is not assignable to type '"center" | "initial" | "inherit" | "unset" | "flex-start" | "flex-end" | "space-between" | "space...'.

Is this something you have encountered?

@pelotom
Copy link
Member Author

pelotom commented Oct 19, 2017

@stevegeek I'm going to guess your code looks something like this:

const decorate = withStyles(theme => ({
  root: {
    justifyContent: 'center'
  }
}));

The problem is that TypeScript is inferring the type of justifyCenter to be string, even though you'd hope it would be able to figure out that you want it to have the literal type center. There are a number of ways to nudge it so it figures things out:

const styles: StyleRulesCallback<'root'> = theme => ({
  root: {
    justifyContent: 'center'
  }
});

const decorate = withStyles(styles);
const decorate = withStyles(theme => ({
  root: {
    justifyContent: 'center'
  } as React.CSSProperties
}));
const decorate = withStyles<'root'>(theme => ({
  root: {
    justifyContent: 'center'
  }
}));

@stevegeek
Copy link

@pelotom Interesting, thanks for that. I wonder why it seems to be able to infer the string as the type literals in certain cases and not others (eg textAlign seems fine`). Anyway thats for the examples, very useful!

@pelotom
Copy link
Member Author

pelotom commented Oct 19, 2017

I opened a TypeScript bug about this: microsoft/TypeScript#19360

@pelotom
Copy link
Member Author

pelotom commented Oct 19, 2017

@stevegeek textAlign "works" because they gave up on trying to make it type safe:

textAlign?: CSSWideKeyword | any;

Note the | any 😕

@stevegeek
Copy link

@pelotom Great, interesting to see what they say

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.

3 participants