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.

Better TypeScript setup for themes

See original GitHub issue

I would like to suggest an improved TypeScript theming setup.

Currently, the docs suggest duplicating your theme: creating one in theme.ts with the values and another in a styled.d.ts declarations file. This has current setup has two limitations.

  1. It isn’t DRY - duplication over these two seperate files is annoying to type out.
  2. Types are vague - You should not only get the name and type (e.g. string) in your autocomplete but also the value (e.g. hsl(200, 12%, 17%) ).

I’ve found a setup that works better for me, which you can find here on my personal website. This setup has less duplication, keeps all your theming in one file and gives you more informative autocomplete.

First, you type-cast your values so that you get more informative autocomplete:

const theme = {
  // blues
  b100: 'hsl(221,100%,11%)' as 'hsl(221,100%,11%)',
  b200: 'hsl(212,80%,20%)' as 'hsl(212,80%,20%)',
  b300: 'hsl(207,84%,24%)' as 'hsl(207,84%,24%)',
  b400: 'hsl(208,60%,35%)' as 'hsl(208,60%,35%)',
  b500: 'hsl(206,61%,40%)' as 'hsl(206,61%,40%)',
  b600: 'hsl(205,53%,48%)' as 'hsl(205,53%,48%)',
  b700: 'hsl(205,56%,57%)' as 'hsl(205,56%,57%)',
  b800: 'hsl(206,68%,71%)' as 'hsl(206,68%,71%)',
  b900: 'hsl(208,82%,85%)' as 'hsl(208,82%,85%)',
}

Second, you import this theme file and extend it, rather than typing out all your values again:

// import original module declarations
import 'styled-components'
// import your custom theme
import theme from '../utils/theme'

// extend the module declarations using custom theme type

type Theme = typeof theme

declare module 'styled-components' {
  export interface DefaultTheme extends Theme {}
}

That’s it!

I think this is a much better way to type your theme and I’d be happy to go and make a PR for the docs if the team agrees.

My one question is about interface DefaultTheme extends Theme {}. This gives me a eslint error with my current setup that warns me about empty themes. However, I can’t think of a better way to rename Theme to DefaultTheme so I’m sticking with it for now.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:152
  • Comments:7

github_iconTop GitHub Comments

10reactions
andy-hookcommented, May 21, 2019

Just wanted to say thanks for writing this up, it was immediately useful and does exactly what I need!

7reactions
zbeyenscommented, Nov 22, 2019

We can remove the duplicate code:

const theme = {
  // blues
  b100: 'hsl(221,100%,11%)' as 'hsl(221,100%,11%)',
  b200: 'hsl(212,80%,20%)' as 'hsl(212,80%,20%)',
  b300: 'hsl(207,84%,24%)' as 'hsl(207,84%,24%)',
  b400: 'hsl(208,60%,35%)' as 'hsl(208,60%,35%)',
  b500: 'hsl(206,61%,40%)' as 'hsl(206,61%,40%)',
  b600: 'hsl(205,53%,48%)' as 'hsl(205,53%,48%)',
  b700: 'hsl(205,56%,57%)' as 'hsl(205,56%,57%)',
  b800: 'hsl(206,68%,71%)' as 'hsl(206,68%,71%)',
  b900: 'hsl(208,82%,85%)' as 'hsl(208,82%,85%)',
}

For a flat object, use a single enum:

image

For nested objects, use multiple enums inside the theme object.

For nested arrays, use as const: image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Theme with styled-components and Typescript | RBI Tech
This article will give some breakdown of the basic setup and naming convention ... and how to setup Typescript with styled-components themes ......
Read more >
TypeScript - Theme UI
The Theme type represents all possible themes. It might be what you need when you're writing a library of reusable components or an...
Read more >
Themes within themes with React, TypeScript and Styled ...
Now we have our basic component, we need to set up its theme with Styled Components' ThemeProvider . In this case, we created...
Read more >
Ultimate Vim TypeScript Setup | Pragmatic Pineapple
VSCodeVim lets you combine the best of two worlds, and this is what I was using in my Visual Studio Code setup. Upgrading...
Read more >
Themes in React with Typescript, Context API, React hooks ...
Another step is to create good typing and theme configuration. Implementation. Types definition. First of all, let's define a Theme interface.
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