question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

styled types do not support object notation

See original GitHub issue

Current behavior:

styled types do not support object notation.

According to the documentation (https://emotion.sh/docs/typescript#passing-props), it should be possible to write

interface ImageProps {
  src: string;
  width: number;
};

const Image1 = styled('div')(
  {
    backgroundSize: 'contain',
  },
  (props: ImageProps) => ({
    width: props.width,
    background: `url(${props.src}) center center`,
  })
);

but it give me that error locally

Argument of type '{ backgroundSize: string; }' is not assignable to parameter of type 'TemplateStringsArray'.
Object literal may only specify known properties, and 'backgroundSize' does not exist in type 'TemplateStringsArray'.ts(2345)

Looking at the types test (https://github.com/emotion-js/emotion/blob/master/packages/styled/types/tests.tsx) it only seems that string literals are supported.

Where types for object notation supported? Is it a bug?

Environment information:

"@emotion/core": "10.0.14"
"@emotion/is-prop-valid": "0.8.2"
"@emotion/styled": "10.0.14"
"react": "^16.8.6"

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Andaristcommented, Jun 29, 2019

The preferred way to pass a props type to styled is:

const Image = styled("div")<ImageProps>(
  {
    backgroundSize: "contain",
    zIndex: 30
  },
  (props) => ({
    width: props.width,
    background: `url(${props.src}) center center`
  })
);

It’s the most “correct one” - because it’s a central place where you can put it and thus have it available in all interpolations automatically.

Also when dealing with overloads TS always tries to match top-down and if it doesn’t find a match then it reports the error about the last unmatched overload. I suspect that it couldn’t match the first one for you because you have annotated props inside your interpolation, but that typing was too restrictive (? not sure about exact reason though) and it didn’t match against “expected” Props & { theme: any }. It then has tried to match against the second one but that one failed quickly because the first argument was not a template.

2reactions
Andaristcommented, Jun 29, 2019

Which tsconfig.json are you using? I can always reproduce it

Hard to tell exactly - codesandbox uses CRA template, so it should be some shape of this. Please provide a repository with the issue reproduced so we can reliably investigate this.

@eriktoyra

Can’t reproduce your issue on codesandbox. Please provide a repository with the issue reproduced so we can reliably investigate this 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object Styles - Emotion
Object styles are especially useful with the css prop because you don't need a css call like with string styles but object styles...
Read more >
API Reference - styled-components
Returns a StyledComponent that does not accept children. Place it at the top of your React tree and the global styles will be...
Read more >
Documentation - Everyday Types - TypeScript
In this chapter, we'll cover some of the most common types of values you'll find in JavaScript code, and explain the corresponding ways...
Read more >
Google TypeScript Style Guide
Syntax : Identifiers: File encoding: UTF-8: No line continuations: Comments & ... Type coercion: Variables: Exceptions: Iterating objects: Iterating ...
Read more >
Styled-components vs. Emotion for handling CSS
This library supports both string and object styles. ... types of styling methods, the framework-agnostic approach does not require external ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found