I am new to Typescript and trying to mimic styled-components at a much lower scale. In my case, I want to make children required if user passed true
in createStyledComponent for children parameter, otherwise make the children optional.
I kinda have a newbie solution to my problem where I am returning 2 different functionsm, one with required children and other with optional.
Is there a better and concise way to do this task?
const createStyledComponent = (
type: any,
component_style: CSSProperties | undefined,
children?: boolean
) => {
if (children) {
const returnComponent = ({
children,
style,
}: {
children: any;
style?: CSSProperties | undefined;
}) => {
return React.createElement(
type,
{ style: { ...component_style, ...style } },
children
);
};
return returnComponent;
} else {
const returnComponent = ({
children,
style,
}: {
children?: any;
style?: CSSProperties | undefined;
}) => {
return React.createElement(
type,
{ style: { ...component_style, ...style } },
children
);
};
return returnComponent;
}
};
2
Answers
I would probably try and take a stab at generics honestly. https://www.typescriptlang.org/docs/handbook/2/generics.html
Something like the following might work for your use case.
But your implementation details might vary. I definitely would stray away from big if, else render blocks that are largely the same. In that type of scenario it can be helpful to abstract these blocks into separate components, or do inline conditionals
Good luck!
You want function overloads.
You have two signatures here:
One where
children
are required:And one where they are not:
Then you can write the implementation as follows:
Note that the
children
parameter to your function is gone in the implementation. It has no use at runtime since the logic is identical, but it does allows us to give the type system a tip and enforce what is required.Now to test that out:
See playground