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.

How to use theme object with typescript and @emotion/styled

See original GitHub issue

At the moment doing something like below throws an error Property colors does not exist in Object

import styled from '@emotion/styled'

const Div = styled.div`
    background: ${({theme}) => theme.colors.brand}
`

I’ve tried many approaches and nothing works but I’ve seen in other issues the library supports Typescript. Is there anything I am missing or doing incorrectly?

Cheers and thank your such an amazing piece of work.

Issue Analytics

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

github_iconTop GitHub Comments

13reactions
ofhousecommented, Jun 29, 2021

What hinders you from creating the types from the theme?

// theme.ts
import { extendTheme, withDefaultColorScheme } from '@chakra-ui/react';

const theme = extendTheme(
  {
    colors: {
      brand: {
        500: '#000000',
      },
    },
  },
  withDefaultColorScheme({ colorScheme: 'brand' })
);

type Theme = typeof theme;

export type { Theme };
export { theme };
// emotion.d.ts
import '@emotion/react';
import { Theme as CustomTheme } from './theme';

declare module '@emotion/react' {
  export interface Theme extends CustomTheme {}
}

Usage:

import styled from '@emotion/styled'

const Div = styled.div`
    background: ${({theme}) => theme.colors.brand}
`
7reactions
asterikxcommented, May 1, 2020

Hi, I want to add custom variants/styles and have a fully-typed theme. How can I use an extended chakra-ui theme with TypeScript? I can’t find a follow-up issue on this @segunadebayo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript
Emotion includes TypeScript definitions for @emotion/react and @emotion/styled . These definitions infer types for css properties with the object syntax, ...
Read more >
EmotionJS with typescript doesn't pass theme type to props ...
Update regarding to unable override Theme via the custom type file. As I've seen the styled type only consume Theme object from @emotion/react ......
Read more >
Styling a React TypeScript application with Emotion - Elliot Laws
In this post, I'll explain the process that was used to setup and add styles into the app using Emotion. I'll also cover...
Read more >
TypeScript
Emotion includes TypeScript definitions for @emotion/react and @emotion/styled . These definitions also infer types for css properties with the object ...
Read more >
How it Works
Emotion : used to generate isolated CSS with theming context · MDX: used to provide custom Emotion styled components to MDX documents, without...
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