1

I have a simple button component, I am styling it with styled components, but I can not send a simple prop to the component as TypeScript complains with No overload matches this call

App.tsx

import React from 'react';

import Button from '../form/button';

export const App = (): React.ReactElement => <Button secondary>Click me</Button>;

Button.tsx

import React from 'react';
import styled from 'styled-components';

const StyledButton = styled.button`
  background-color: #333;
  padding: 10px 20px;
  color: ${({ secondary }) => (secondary ? 'white' : 'red')};
`;

interface Props {
  children: string;
  secondary?: any;
}

const Button: React.FC<Props> = ({ children, secondary }) => (
  <StyledButton secondary={secondary}>{children}</StyledButton>
);

export default Button;

This should work, what does TypeScript needs me to specify? The error is not very helpful

5
  • try <Button secondary={'true'}>Click me</Button> Commented Jan 16, 2021 at 13:07
  • @Sam, still says No overload matches this call Commented Jan 16, 2021 at 13:09
  • @Álvaro children: React.ReactNode and secondary: boolean Commented Jan 16, 2021 at 13:10
  • @RameshReddy, same No overload matches this call Commented Jan 16, 2021 at 13:12
  • codesandbox.io/s/headless-silence-uwv7z?file=/src/Button.tsx here is the working code Commented Jan 16, 2021 at 13:13

2 Answers 2

1

It's because you haven't passed the props to the styled component, so it does not know what secondary is.

You can pass your props like so (and I removed children since you don't need to specify this manually)

import React from "react";
import styled from "styled-components";

interface Props {
  secondary?: any;
}

const StyledButton = styled.button<Props>`
  background-color: #333;
  padding: 10px 20px;
  color: ${({ secondary }) => (secondary ? "white" : "red")};
`;


const Button: React.FC<Props> = ({ children, secondary }) => (
  <StyledButton secondary={secondary}>{children}</StyledButton>
);

export default Button;

CodeSandbox: https://codesandbox.io/s/determined-worker-62xtm?file=/src/form/Button.tsx

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, It turns out I still needed to change the type to boolean on secondary for it to work, but with that and your changes is good now
Yes, boolean is what you should be using. ;)
0

You can just specify that the props passed and then destructured are from the interface Props and access the parameter secondary.

const StyledButton = styled.button`
  background-color: #333;
  padding: 10px 20px;
  color: ${({secondary}: Props) => (secondary ? 'white' : 'red')}
`;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.