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.

Custom radio buttons example does not work in TypeScript

See original GitHub issue

🐛 Bug report

I’m trying to use the Chakra UI custom radio buttons example for my TypeScript React project.

💥 Steps to reproduce

Copy the example component into your project, adding the necessary props type:

function RadioCard(props: PropsWithChildren<UseRadioProps>) {
    const { getInputProps, getCheckboxProps } = useRadio(props);

    const input = getInputProps();
    const checkbox = getCheckboxProps();

    return (
        <Box as="label">
            <input {...input} />
            <Box
                {...checkbox}
                cursor="pointer"
                borderWidth="1px"
                borderRadius="md"
                boxShadow="md"
                _checked={{
                    bg: "teal.600",
                    color: "white",
                    borderColor: "teal.600",
                }}
                _focus={{
                    boxShadow: "outline",
                }}
                px={5}
                py={3}
            >
                {props.children}
            </Box>
        </Box>
    );
}

Experience the following compilation error:

error TS2322: Type '{ children: ReactNode; cursor: "pointer"; borderWidth: "1px"; borderRadius: "md"; boxShadow: "md"; _checked: 
{ bg: string; color: string; borderColor: string; }; _focus: { boxShadow: string; }; ... 256 more ...; css?: CSSProp<...> | undefined; }' is not assignable to type 'ChakraProps'.
    Types of property 'css' are incompatible.
      Type 'CSSProp<any> | undefined' is not assignable to type 'Interpolation<{}>'.
        Type 'FlattenInterpolation<ThemeProps<any>>' is not assignable to type 'Interpolation<{}>'.
          Type 'readonly Interpolation<ThemeProps<any>>[]' is missing the following properties from type 'ArrayInterpolation<{}>': pop, push, reverse, shift, and 6 more.

🧐 Expected behavior

The example should work in a TypeScript project.

🌍 System information

Software Version(s)
Chakra UI 1.6.1
Browser N/A
Operating System Windows

📝 Additional information

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
segunadebayocommented, Aug 9, 2021

Hi @rossng,

This error is very common when you have multiple instances of emotion in node_modules. Storybook is usually the suspect here since they use the older versions of emotion which pollutes the TS namespace with that css type definition.

I’d recommend creating a .yarnclean file and include @emotion/core/types to fix that type definition. You can so try @CaitlinSweeney’s solution (thank you, Caitlin!)

If the issue persists, please send a reproduction link.

2reactions
CaitlinSweeneycommented, Aug 3, 2021

I’ll leave this here if it helps someone else, not sure what your children look like-

interface YourProps extends UseRadioProps {
  children: React.ReactElement | string;
}

const RadioCheck = (props: YourProps): React.ReactElement => {
  const { getInputProps, getCheckboxProps } = useRadio(props);
  const input = getInputProps();
  const { children, isChecked } = props;

I think I had a similar error @mjgwood. Did you try using the UseRadioProps?

Also, check to make sure your value is a string, ie: {...getRadioProps({ value: someValue.toString() })}

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Customized Radio Button Input does not work as ...
I created a customized radio button with help of styled-components and react-typescript The working code is here on codesandbox.
Read more >
How to Create a Reusable Custom Radio Button in React with ...
1. Ensure good usability · 2. Readability is crucial · 3. Radio buttons are for selection only · 4. Always define a default...
Read more >
Typescript error when using custom radio group with ... - GitHub
I came across this discussion which has a working solution on codesandbox but it also shows the same error. I cannot decipher this,...
Read more >
Don't Fear Custom Radio Buttons - DEV Community ‍ ‍
It is a set of checkable buttons, known as radio buttons, where no more than one of the buttons can be checked at...
Read more >
How to get value of selected radio button using JavaScript?
To get the value of the selected radio button, a user-defined function can be created that gets all the radio buttons with the...
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