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

Can't reuse portions of styles in styled components #1373

Closed
ingver opened this issue May 31, 2019 · 1 comment
Closed

Can't reuse portions of styles in styled components #1373

ingver opened this issue May 31, 2019 · 1 comment

Comments

@ingver
Copy link

ingver commented May 31, 2019

Current behavior:

When creating a styled component, Typescript compiler gives an error if providing a few variables of object-styles as arguments, while no errors occured when providing object literals (which types somehow inferred correctly). In first case compiler decides that the first argument should be of TemplateStringsArray type.

To reproduce:

Reproduced this kind of error in codesandbox
Lines 9 - 34

Expected behavior:

No errors should be occured while creating styled components both with untyped objects from variables and with object literals.

Environment information:

  • react version: 16.8.6
  • emotion version: 10.0.9
@Andarist
Copy link
Member

Andarist commented Jun 2, 2019

This works "as expected" (not in terms of emotion, but TS itself).

TS has something called type widening, so your fooStyles has a type of {display: string, flexDirection:string} and that's too "broad" to satisfy CSSType because it rightfully uses enumerated strings for values of properties such as display and flexDirection.

Fortunately TS has introduced const assertions lately and you should be able to use this feature to make it work with:

const fooStyles = {
  display: 'flex',
  flexDirection: 'column'
} as const;

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

2 participants