I am trying to define the correct icon prop for the Button component. This prop could be an object, array, and string.
Button.tsx
import React from 'react'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { SizeProp, IconProp } from '@fortawesome/fontawesome-svg-core'
export interface IconType {
name: IconProp
size: SizeProp
color?: string
}
export interface ButtonProps {
icon: IconType | IconProp
}
const Button: React.FC<ButtonProps> = ({ icon }: ButtonProps) => {
return (
<button>
<FontAwesomeIcon size={icon.size} icon={icon.name || icon} color={icon.color} />
</button>
)
}
export default Button
Well, this are possible values for icon prop for the Button component
import { faHome } from '@fortawesome/free-solid-svg-icons'
<Button icon="home" /> // Icon prop as string
<Button icon={["fab", "github"]} /> // Icon prop as array string with only two positions
<Button icon={{ color: "red", size: "2x", name: "home" }} /> // Icon prop as object with only "color, size, name" properties, color property can to be optional
<Button icon={faHome} /> // Icon prop as imported icon from library fortawesome
Well, the problem is, that I would like to use all the possible values, but I have an error message: Property 'size' does not exist on type 'IconType | IconProp'
It directs me to the IconProp source and notice that it has the following
export type IconPrefix = "fas" | "fab" | "far" | "fal" | "fad";
export interface IconLookup {
prefix: IconPrefix;
// IconName is defined in the code that will be generated at build time and bundled with this file.
iconName: IconName;
}
export type IconName = '500px' |
'accessible-icon' |
'accusoft' |
'home' |
'acquisitions-incorporated' | ... more icons
type IconProp = IconName | [IconPrefix, IconName] | IconLookup
Well, so, How I can resolve this error? I'm not 100% proficient in typescript yet, I hope to learn a lot by solving this. Thank you very much