In Typescript, I set up a component as follows:
interface MyComponentProps {
type: 'round' | 'square';
}
const MyComponent: FC<MyComponentProps> = ({type = 'round'}) => {
return (
<div />
);
};
The type prop is required and has a default set in the component definition, but still I get and error when calling the component:
<MyComponent />
// Property 'type' is missing in type '{ }' but required in type 'MyComponentProps'.
Setting the property type to an optional type? solves the problem by implicitly changing the type to 'round' | 'square' | undefined but I don't want the property to be possibly undefined, because that would cause issues and weird code down the line where I must consider type being undefined at every point.
What do I want to happen?
I want 'type' to have a default value when not passed, but not be defined as undefined (i.e. optional).
What have I tried?
I tried adding
MyComponent.defaultProps = {
type: 'round'
};
But this didn't help at all, and also I know that defaultProps are about to become deprecated for functional components anyway.