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.

Want TypeScript error for when css prop is passed a function (instead of SerializedStyles)

See original GitHub issue

The problem

When using Emotion, I mostly create “static” styles in variables for use in my JSX to avoid cluttering the JSX with style information. For example:

const style = css({ fontSize: 40 });
const Component = () => <button css={style}>content</button>;

Occasionally I need to create “dynamic” styles, based off of the component’s props:

const style = size => css({ fontSize: size });
const Component = props => <button css={style(props.size)}>content</button>;

A common mistake I make is forgetting to call style() as a function, and instead writing css={style}, which of course doesn’t apply any styles because the style() function is never called.

I was hoping that using TypeScript with Emotion would help me avoid this error, but Emotion’s typings for the css prop seem to allow the function. I wish the typings would warn me.

Here’s an example: https://codesandbox.io/s/mystifying-keldysh-bdker

Proposed solution

Emotion’s type definitions should cause a TypeScript error when I pass a function to the css prop.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
JakeGinnivancommented, Oct 30, 2019

A manual backwards compat step would be to add instructions to do this:

declare module "@emotion/core" {
  interface DefaultTheme {
    [key: string]: any
  }
}
1reaction
Andaristcommented, Oct 28, 2019

With vanilla emotion you won’t have eassy theme access though. I think we are going to go with DefaultTheme approach in the end, but we’d also like to promote creating your own themes with https://github.com/emotion-js/emotion/pull/1577 to avoid problems described in https://github.com/emotion-js/emotion/issues/1507#issuecomment-539919716 and in https://github.com/emotion-js/emotion/pull/973 . This all is still in the works though so stay tuned.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript error when passing a function as a prop that returns ...
It looks like it doesn't like the fact that I pass a callback to the sx prop, then run a function inside it....
Read more >
TypeScript - Emotion
You can now define styles using the object syntax or template literal syntax and pass them to your components via the css prop....
Read more >
Emotion - TypeScript
TypeScript checks css properties with the object style syntax using ... as our jsx factory function takes provided css prop, resolves it and...
Read more >
property 'css' does not exist on type 'intrinsicattributes - You.com
Typescript error : Type '{ onClick: () => void; }' is not assignable to type ... For functional components I usually use function...
Read more >
API - React Select
These base props are those available to be passed to all select variants. ... This is useful when styling via CSS classes instead...
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