SolidJSS
SolidJSโ€ข4y agoโ€ข
2 replies
Ninguern

How to properly type the `mergeProps` with Typescript?

I'm using the mergeProps function to enter some default props for my component, but this is causing a Typescript error because some properties that are using string literals are being replaced with just type
string
.

interface ButtonBaseProps extends JSX.ButtonHTMLAttributes<HTMLButtonElement> {
  variant?: ButtonVariant;
  color?: ColorNames;
}

function ButtonBase(props: ButtonBaseProps) {
  const p = mergeProps({ variant: "filled", type: "button" }, props);

  return (
    <button
      class={join(
        "flex items-center rounded-full py-2 outline-none",
        getButtonStyles(p.variant, p.color),
        p.children ? "px-4" : "px-2"
      )}
      {...p}
    />
  );
}


<button is trowing an error because p.type is now typed as a string.
Type 'string' is not assignable to type '"button" | "submit" | "reset" | undefined'.

getButtonStyles is trowing an error because p.variant is now a string instead of ButtonVariant.
Argument of type 'string' is not assignable to parameter of type 'ButtonVariant'.

Is there a way where I can type this to solve this issue?
Was this page helpful?