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.

Typescript Type support for style objects

See original GitHub issue

I’m working with React Typescript and Emotion. So far I have used template strings, but I’d like to gradually move to style objects. I’d like to be able to declare a style object, get type support from the editor, and then pass it to a css function, or a styled component. CSSProperties type by @types/react provides such support (only valid css properties could be keys). but emotion css function doesn’t accept parameters of that type. It does accept CSSObject (by Emotion), but that type accepts any key. The solution I currently use is:

export type CSSObj = Pick<CSSObject, keyof CSSProperties>

This works well. Is there a “native” emotion way to do this? If there isn’t, perhaps such a type could be added. I’d be happy to prepare a PR.

  • emotion version: 9.2.12
  • react version: 16.7

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
thisissamicommented, Jul 5, 2019

Just one update: this is the type that I use, if anybody is curious.

import { CSSPropertiesWithMultiValues } from '@emotion/serialize';

export type CSSObj = CSSPropertiesWithMultiValues & { label?: string };
0reactions
Andaristcommented, Oct 31, 2019

So to sum it up - if this is still something that is problematic, please raise a PR against #1501. Other than that I don’t think there is anything else actionable here right now, so I’m going to close the issue to clean up the issue tracker.

Be sure though that we care about DX a lot so if this still somehow bothers you and you cannot raise a PR yourself then please open a new issue explaining the exact problem and ideally with proposed solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type for style attribute passed to function - Stack Overflow
I want to know how to properly declare it so that it checks the types of its arguments the way typescript does for...
Read more >
TypeScript and React: Styles and CSS - fettblog.eu
Every React HTML element has a style property that allows an object with all your styling. ... TypeScript support comes through DefinitelyTyped :...
Read more >
Documentation - Creating Types from Types - TypeScript
An overview of the ways in which you can create more types from existing types.
Read more >
Rosebox: CSS in Typescript - Level Up Coding - gitconnected
Rosebox is a CSS-in-Typescript library that provides features like strong types(e.g., Angle ), typed functions (e.g., linearGradient ) ...
Read more >
Using styled-components in TypeScript: A tutorial with examples
Here, you can learn how to build and style a TypeScript app using ... One of the best things about styled-components is that...
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