I am working on React-TypeScript app, creating credit card number imput component. While the user is entering a credit card number, the FontAwesome icon inside input should update to the brand image. I'm getting this error:
Types of property 'icon' are incompatible.
Type 'string' is not assignable to type 'IconProp'
Already tried this solution that didn't worked for me: https://github.com/FortAwesome/react-fontawesome/issues/143#issuecomment-410677960
I thinks this is TypeScript issue, wrong types being passed to icon prop.
This is simplified version of my tsx file, maybe it helps:
import { faCreditCard } from '@fortawesome/fontawesome-free-solid';
import { faCcMastercard, faCcVisa } from '@fortawesome/free-brands-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
const CARD_TYPES: { [index: string]: string } = {
mastercard: 'MASTERCARD',
visa: 'VISA',
};
const CARD_ICONS: { [index: string]: any } = {
mastercard: faCcMastercard,
placeholder: faCreditCard,
visa: faCcVisa,
};
interface IProps {
placeholder: string;
icon: string;
}
interface IState {
cardIcon: string;
}
export default class CardNumber extends Component<IProps,IState
> {
constructor(props: IWmxCardNumberProps) {
super(props);
this.state = {
cardIcon: CARD_ICONS.placeholder,
};
}
componentDidMount() {
this.setState({
cardIcon: CARD_ICONS[cardType] || CARD_ICONS.placeholder
});
}
onCardNumberChange(e: any) {
this.setState({
cardIcon: CARD_ICONS[cardType] || CARD_ICONS.placeholder
});
}
public render() {
const { cardIcon } = this.state;
const { placeholder } = this.props;
return (
<Container>
<FieldWrapper>
<InputWrapper data-max="9999 9999 9999 9999 9999">
{/* Error: Types of property 'icon' are incompatible. Type 'string' is not assignable to type 'IconProp' */}
<FontAwesomeIcon icon={cardIcon} className="credit-card-icon" />
<Input
type="tel"
placeholder={placeholder}
onChange={this.onCardNumberChange}
/>
</InputWrapper>
</FieldWrapper>
</Container>
);
}
}
So how can i fix this error?
you need to change this:
interface IState {
cardIcon: IconProp;
}
The error you are getting is because the icon prop can only take a subset of string which is IconProp.
You can look at the definition here which includes the type IconName in this file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With