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

Typing of white-space #1025

Closed
e11en opened this issue Nov 19, 2018 · 7 comments
Closed

Typing of white-space #1025

e11en opened this issue Nov 19, 2018 · 7 comments

Comments

@e11en
Copy link

e11en commented Nov 19, 2018

  • emotion version: 9.2.4
  • react version: 16.4.0
  • react-emotion version: 9.2.4

When using a mixin to fill my styled component the variable needs to be whitespace (lower case S) while in the normal use of white-space I need to use whiteSpace (upper case S). I was banging my head for a while trying to fix the error it produced. I think the typing somewhere needs to be fixed.

export const nowrap = {
  whitespace: 'nowrap'
};

// With use of mixin
const Container = styled('div')(({theme}: {theme: Theme}) => ({
     ...nowrap
}));

// Normal use without mixin
const Container = styled('div')(({theme}: {theme: Theme}) => ({
     whiteSpace: 'nowrap'
}));

export const StyledDiv = compose<any, any>(
  withTheme()
)(Container);
@Ailrun
Copy link
Member

Ailrun commented Nov 19, 2018

Which one do you use? TS or Flow?

@e11en
Copy link
Author

e11en commented Dec 10, 2018

Typescript

@Ailrun
Copy link
Member

Ailrun commented Dec 10, 2018

Then it's because you didn't put exact type.
whiteSpace accepts some specific strings only, where TS infer { whiteSpace: string } for your use case. Giving appropriate type will fix this.

@e11en
Copy link
Author

e11en commented Dec 11, 2018

I don't understand your comment. The whole point is that the typing isn't correct when I use a mixin. The type should be whiteSpace but it only works with whitespace.

@Ailrun
Copy link
Member

Ailrun commented Dec 11, 2018

@e11en whitespace works since typing should accept any string key since there is something like & > &, & > span, ... and so on (and even CSS variables!).
If my previous comment is not understandable to you, just check following example TS code.
TS playground

It's just how TS type interference works.

@Ailrun
Copy link
Member

Ailrun commented Feb 1, 2019

microsoft/TypeScript#29510
This TS feature will help you to solve this issue in an easier way.

@Andarist
Copy link
Member

As described - to fix the issue you need to use const assertions to narrow down the type from string to a concrete string. This is not an issue specific to emotion itself - it's just a TS stuff.

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

No branches or pull requests

3 participants