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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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:or with a custom theme:
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?