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.

TS: ChakraTheme typings cause some errors

See original GitHub issue

🐛 Bug report

When using the ChakraTheme type, colors aren’t correctly typechecked.

💥 Steps to reproduce

import { ChakraTheme, useTheme } from "@chakra-ui/react";

export default function MyComponent() {
  const theme = useTheme<ChakraTheme>();
  console.log(theme.colors.purple[100]); // type error
  // ...
TS7053: Element implicitly has an 'any' type because expression of type '100' can't be used to index type
'RecursiveProperty<string | Record<string, Partial<ColorHues>>>'.   Property '100' does not exist on type
'RecursiveProperty<string | Record<string, Partial<ColorHues>>>'.

💻 Link to reproduction

I tried to create a reproduction, but the error did not show up in CodeSandbox…

🌍 System information

Software Version(s)
Chakra UI 1.6.2
Browser Chrome 90.0.4430.212
Operating System Windows 10

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TimKolbergercommented, Jun 3, 2021

The type ChakraTheme is an interface, which describes how a Chakra UI theme looks like. It does not reflect the types for the default theme or your custom theme.

The error your are reporting is coming from the loose object structure we allow in the ChakraTheme. You could create a custom theme which only has this colors setup:

const myVeryCustomTheme: ChakraTheme = {
  colors: {
    purple: 'rebeccapurple'
  }
  //...
}

With this theme in place this would fail: theme.colors.purple[100].

Use the type Theme for the default theme typings or create your own type for your custom theme:

import { Theme, useTheme } from '@chakra-ui/react'

export default function MyComponent() {
  const theme = useTheme<Theme>();
  console.log(theme.colors.purple[100]); 
  // ...

or with a custom theme:

import { extendTheme, useTheme } from '@chakra-ui/react'

const customTheme = extendTheme({ /*...*/ }) // use this in <ChakraProvider theme={customTheme} />
type CustomTheme = typeof customTheme

export default function MyComponent() {
  const theme = useTheme<CustomTheme>();
  console.log(theme.colors.purple[100]); 
  // ...
0reactions
Inayat-Ccommented, Apr 23, 2022

Am I missing something? As per the above instructions I’m trying to get access to my custom theme typings but it doesn’t seem to work?

Screenshot 2022-04-23 at 19 41 11
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix Node.js puppeteer keyboard.type() causes TypeError
type() in order to isolate the problem. I'm writing with typescript and running the files with ts-node. The same error was raised on...
Read more >
CLI - Chakra UI
Troubleshoot: If the theme typings don't show up immediately, try restarting your TypeScript server ( Cmd + Shift + P > "TypeScript: Restart...
Read more >
Typing Errors - Reason Magazine
Typing Errors. The standard typewriter keyboard is Exhibit A in the hottest new case against markets. But the evidence has been cooked.
Read more >
Search function problem in Frontity Chakra Theme - Get Help
I have a little problem in search function. You can test? Blank page when triying to search and see this in console: TypeError:...
Read more >
@chakra-ui/cli - npm
Generate theme typings for autocomplete. Latest version: 2.2.0, ... (js|ts)>. or. npx @chakra-ui/cli tokens <@your-org/chakra-theme-package>.
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