react-fontawesome: Type 'IconDefinition' is not assignable to type 'IconProp' with Typescript

I’m using typescript and next.js react project.

I was trying to import github icon(faGithub) in fontawesome.

when I run the next.js’s dev mode first without import github icon (no error happens in typescript) and trying to import github icon in fontawesome, it works well.

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faGithub } from '@fortawesome/free-brands-svg-icons';

...

<FontAwesomeIcon icon={faGithub} className={sizeClass} />

but problem is to execute typescript compiler(tsc). tsc throws error like this

error TS2322: Type 'IconDefinition' is not assignable to type 'IconProp'.
  Type 'IconDefinition' is not assignable to type 'IconLookup'.
    Types of property 'iconName' are incompatible.
      Type 'import("/[path-to-my-project]/node_modules/@fortawesome/free-brands-svg-icons/node_modules/@fortawesome/fontawesome-common-types/index").IconName' is not assignable to type 'import("/[path-to-my-project]/node_modules/@fortawesome/fontawesome-common-types/index").IconName'.
        Type '"deezer"' is not assignable to type 'IconName'.

26         <FontAwesomeIcon icon={faGithub} className={sizeClass} />

I’d tried using some ways, tsc passed. but there’s not working in actual react project

  • <FontAwesomeIcon icon={[‘fab’, ‘github’]} className={sizeClass} />
  • <FontAwesomeIcon icon={[‘fas’, ‘github’]} className={sizeClass} />
Could not find icon {prefix: "fab", iconName: "github"} 

Could not find icon {prefix: "fas", iconName: "github"} 

so I tried this way to make it work the tsc and actual react project.

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { IconProp } from '@fortawesome/fontawesome-svg-core';
import { faGithub } from '@fortawesome/free-brands-svg-icons';

...

const faPropIcon = faGithub as IconProp;

...

<FontAwesomeIcon icon={faPropIcon} className={sizeClass} />

my package.json

"@fortawesome/fontawesome-svg-core": "^1.2.28",
"@fortawesome/free-brands-svg-icons": "^5.14.0",
"@fortawesome/free-solid-svg-icons": "^5.13.0",
"@fortawesome/react-fontawesome": "^0.1.9",

I think it’s kind of weird.

because this problem happens after installing @fortawesome/free-brands-svg-icons and use faGithub in @fortawesome/free-brands-svg-icons.

is it bug? or should I use this way like this continuously?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 49
  • Comments: 42 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I know this is closed, but if it helps, I did this and it worked. 😃

<FontAwesomeIcon icon={faFacebookF as IconProp} />

Plain incompetence at Fontawesome dev team… and frankly I do not think anybody at Fontawesome even cares, because this is such a dumb mistake.

@karlkaspar I think you should rethink your words, they are really inappropriate.

This is an open and friendly community, there is no need to flame/blame others.

Why not just open a new issue (maybe even a PR) to fix the docs? You can spread your knowlege instead of going toxic.

just import any icon from solid svg module

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faPlus } from '@fortawesome/free-solid-svg-icons';

// inside JSX or TSX
<FontAwesomeIcon icon={faPlus}/>

That’s it, enjoy 🎉

I have come to understand the underlying issue.

I followed the instructions found on Fontawesome website to install their product.

Plain incompetence at Fontawesome dev team… and frankly I do not think anybody at Fontawesome even cares, because this is such a dumb mistake. image From the image we can see how we are supposed to use an icon in react.

This will give you a type error if you are using Typescript, because god forbid anyone at Fontawesome has ever heard of React and Typescript, the most popular web development combo for the past 5 years.

This is how you can actually use it. <FontAwesomeIcon icon={[‘fa’, ‘arrow-right-from-bracket’]} />

The first parameter ‘fa’ can be of these types export type IconPrefix = “fas” | “far” | “fal” | “fat” | “fad” | “fab” | “fak” ;

THERE IS NO ‘FA’ IN THE ALLOWED TYPES… the icon Fontawesome said on their page, that I can use, I CANNOT because of their multiple blunders…

The last parameter can be found in the same file as IconPrefix export type IconName = [ … . … … . ] tons of lines of code here I am not going to paste.

Had the same issue today. Fixed by upgrading packages:

"dependencies": {
    "@fortawesome/fontawesome-svg-core": "^6.1.1",
    "@fortawesome/free-solid-svg-icons": "^6.1.1",
    "@fortawesome/react-fontawesome": "^0.1.18"
}

@Jule- I can confirm this Upgrading all packages to the newest version fixed this for me (especially @fortawesome/fontawesome-svg-core": "^1.3.0 -> @fortawesome/fontawesome-svg-core": "^6.1.0)

Let me update

I upgrade latest version, it works well. error is gone without using as.

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
- import { IconProp } from '@fortawesome/fontawesome-svg-core';
import { faGithub } from '@fortawesome/free-brands-svg-icons';

...

- const faPropIcon = faGithub as IconProp;
...

- <FontAwesomeIcon icon={faPropIcon} className={sizeClass} />
+ <FontAwesomeIcon icon={faGithub} className={sizeClass} />

package.json

"@fortawesome/fontawesome-svg-core": "^1.2.28", -> "^1.2.32",
"@fortawesome/free-brands-svg-icons": "^5.14.0", -> "^5.15.1",
"@fortawesome/free-solid-svg-icons": "^5.13.0", -> "^5.15.1",

yarn.lock

Before

"@fortawesome/fontawesome-common-types@^0.2.28":
  version "0.2.28"
  resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-0.2.28.tgz#1091bdfe63b3f139441e9cba27aa022bff97d8b2"
  integrity sha512-gtis2/5yLdfI6n0ia0jH7NJs5i/Z/8M/ZbQL6jXQhCthEOe5Cr5NcQPhgTvFxNOtURE03/ZqUcEskdn2M+QaBg==

"@fortawesome/fontawesome-common-types@^0.2.30":
  version "0.2.30"
  resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-0.2.30.tgz#2f1cc5b46bd76723be41d0013a8450c9ba92b777"
  integrity sha512-TsRwpTuKwFNiPhk1UfKgw7zNPeV5RhNp2Uw3pws+9gDAkPGKrtjR1y2lI3SYn7+YzyfuNknflpBA1LRKjt7hMg==

"@fortawesome/fontawesome-svg-core@^1.2.28":
  version "1.2.28"
  resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-svg-core/-/fontawesome-svg-core-1.2.28.tgz#e5b8c8814ef375f01f5d7c132d3c3a2f83a3abf9"
  integrity sha512-4LeaNHWvrneoU0i8b5RTOJHKx7E+y7jYejplR7uSVB34+mp3Veg7cbKk7NBCLiI4TyoWS1wh9ZdoyLJR8wSAdg==
  dependencies:
    "@fortawesome/fontawesome-common-types" "^0.2.28"

"@fortawesome/free-brands-svg-icons@^5.14.0":
  version "5.14.0"
  resolved "https://registry.yarnpkg.com/@fortawesome/free-brands-svg-icons/-/free-brands-svg-icons-5.14.0.tgz#98555518ba41bdff82fbae2f4d1bc36cd3b1c043"
  integrity sha512-WsqPFTvJFI7MYkcy0jeFE2zY+blC4OrnB9MJOcn1NxRXT/sSfEEhrI7CwzIkiYajLiVDBKWeErYOvpsMeodmCQ==
  dependencies:
    "@fortawesome/fontawesome-common-types" "^0.2.30"

"@fortawesome/free-solid-svg-icons@^5.13.0":
  version "5.13.0"
  resolved "https://registry.yarnpkg.com/@fortawesome/free-solid-svg-icons/-/free-solid-svg-icons-5.13.0.tgz#44d9118668ad96b4fd5c9434a43efc5903525739"
  integrity sha512-IHUgDJdomv6YtG4p3zl1B5wWf9ffinHIvebqQOmV3U+3SLw4fC+LUCCgwfETkbTtjy5/Qws2VoVf6z/ETQpFpg==
  dependencies:
    "@fortawesome/fontawesome-common-types" "^0.2.28"

After

"@fortawesome/fontawesome-common-types@^0.2.32":
  version "0.2.32"
  resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-0.2.32.tgz#3436795d5684f22742989bfa08f46f50f516f259"
  integrity sha512-ux2EDjKMpcdHBVLi/eWZynnPxs0BtFVXJkgHIxXRl+9ZFaHPvYamAfCzeeQFqHRjuJtX90wVnMRaMQAAlctz3w==

"@fortawesome/fontawesome-svg-core@^1.2.32":
  version "1.2.32"
  resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-svg-core/-/fontawesome-svg-core-1.2.32.tgz#da092bfc7266aa274be8604de610d7115f9ba6cf"
  integrity sha512-XjqyeLCsR/c/usUpdWcOdVtWFVjPbDFBTQkn2fQRrWhhUoxriQohO2RWDxLyUM8XpD+Zzg5xwJ8gqTYGDLeGaQ==
  dependencies:
    "@fortawesome/fontawesome-common-types" "^0.2.32"

"@fortawesome/free-brands-svg-icons@^5.15.1":
  version "5.15.1"
  resolved "https://registry.yarnpkg.com/@fortawesome/free-brands-svg-icons/-/free-brands-svg-icons-5.15.1.tgz#1dc0563f4036639e53d24b8e532ea78a53ca2250"
  integrity sha512-pkTZIWn7iuliCCgV+huDfZmZb2UjslalXGDA2PcqOVUYJmYL11y6ooFiMJkJvUZu+xgAc1gZgQe+Px12mZF0CA==
  dependencies:
    "@fortawesome/fontawesome-common-types" "^0.2.32"

"@fortawesome/free-solid-svg-icons@^5.15.1":
  version "5.15.1"
  resolved "https://registry.yarnpkg.com/@fortawesome/free-solid-svg-icons/-/free-solid-svg-icons-5.15.1.tgz#e1432676ddd43108b41197fee9f86d910ad458ef"
  integrity sha512-EFMuKtzRMNbvjab/SvJBaOOpaqJfdSap/Nl6hst7CgrJxwfORR1drdTV6q1Ib/JVzq4xObdTDcT6sqTaXMqfdg==
  dependencies:
    "@fortawesome/fontawesome-common-types" "^0.2.32"

Let me close this issue?

This has broken again with fontawesome-svg-core@1.3 (while using free-solid-svg-icons@5). It might not be worth reopening though, since it works with the new free-solid-svg-icons@6.

No type errors with v5 and v1.2.36:

yarn list v1.22.17
├─ @fortawesome/fontawesome-common-types@0.2.36
├─ @fortawesome/fontawesome-svg-core@1.2.36
├─ @fortawesome/free-solid-svg-icons@5.15.4
└─ @fortawesome/react-fontawesome@0.1.17

Throws the type errors with v5 and v1.3.0:

yarn list v1.22.17
├─ @fortawesome/fontawesome-common-types@0.2.36
├─ @fortawesome/fontawesome-svg-core@1.3.0
│  └─ @fortawesome/fontawesome-common-types@0.3.0
├─ @fortawesome/free-solid-svg-icons@5.15.4
└─ @fortawesome/react-fontawesome@0.1.17

I’m not sure where the fontawesome-common-types@0.2.36 is being required (probably solid-svg-icons v5). I don’t have it explicitly listed in my package.json. I assume having the two different versions of fontawesome-common-types is the source of the problem.

No type errors with v6 and v1.3.0:

yarn list v1.22.17
├─ @fortawesome/fontawesome-common-types@0.3.0
├─ @fortawesome/fontawesome-svg-core@1.3.0
├─ @fortawesome/free-solid-svg-icons@6.0.0
└─ @fortawesome/react-fontawesome@0.1.17

I’m using Font Awesome on a React Native project and I followed these docs to set it up.

I was getting this issue until I noticed that I missed this install:

npm i --save @fortawesome/fontawesome-svg-core

Adding that package resolved my issue.

Also having the same issue 🤔

Having the same issue as well.

Can you all try something for me (these Typescript issues are so frustrating for everyone)?

Upgrade to the latest dependencies on everything (including @fortawesome/fontawesome-common-types) and give this another go. And if anyone can provide a reproducible test case that would be very helpful. Every time I try to reproduce a TS error I fail so I must be missing a critical piece. We don’t use TS daily at Font Awesome so we have to rely pretty heavily on the community to solve some of these.

This is a dependency issue. Upgrading @fortawesome/free-solid-svg-icons from ^6.2.0 to ^6.2.1 causes its dependency on @fortawesome/fontawesome-common-types 6.2.1 to be installed separately at node_modules/@fortawesome/free-solid-svg-icons/node_modules/@fortawesome/fontawesome-common-types while the existing package @fortawesome/fontawesome-common-types installed at node_modules/@fortawesome/fontawesome-common-types stays at version 6.2.0. Since @fortawesome/react-fontawesome and @fortawesome/free-solid-svg-icons use different versions of the same dependency, their types are incompatible. npm update fixes it, but the error message is not obvious.

I’m having the same issue… It used to work well. Maybe a change in typescript?

@geoseong 's use of “free-brands-svg-icons” helped me. I made the change below (while guessing that "fa-solid fa-caret-down" could be changed to { faCaretDown}. I also had installed free-solid instead of free-brands, but it still worked for me.

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
+ import { faCaretDown } from '@fortawesome/free-solid-svg-icons';
- <FontAwesomeIcon icon='fa-solid fa-caret-down' />
+ <FontAwesomeIcon icon={faCaretDown}/>

I’ve tried almost all the combinations posted here already, no luck. Here’s the latest set of versions that’s breaking for me. Any ideas?

"@fortawesome/fontawesome-common-types": "^6.2.0",
"@fortawesome/fontawesome-svg-core": "^6.2.0",
"@fortawesome/free-solid-svg-icons": "^6.2.0",
"@fortawesome/react-fontawesome": "^0.2.0",

It seems that there is wrong dependency:

  ├─┬ @fortawesome/fontawesome-svg-core@1.3.0
  │ └── @fortawesome/fontawesome-common-types@0.3.0
  ├─┬ @fortawesome/free-regular-svg-icons@6.1.0
  │ └── @fortawesome/fontawesome-common-types@6.1.0
  └─┬ @fortawesome/free-solid-svg-icons@6.1.0
    └── @fortawesome/fontawesome-common-types@6.1.0

If like me you have added these dependencies at the wrong time you should have pulled some in v1 and some in v6. It seems that 6.0.0 => 6.1.0 changed that common-type version while svg-core@1.3.0 sticked not updated since v6 is major release. Take care to install synced versions to solve this issue!

I’m using Font Awesome on a React Native project and I followed these docs to set it up.

I was getting this issue until I noticed that I missed this install:

npm i --save @fortawesome/fontawesome-svg-core

Adding that package resolved my issue.

This worked for me too, thanks @SeanBarker182

As mentioned above by @GeorgEchterling, you should update your packages and set them to the same version.

To verify their versions, type: yarn list | grep fontawesome or npm list | grep fortawesome. This command will return something like this:

yarn list | grep fontawesome
├─ @fortawesome/fontawesome-common-types@6.2.0
├─ @fortawesome/fontawesome-svg-core@6.2.0
│  └─ @fortawesome/fontawesome-common-types@6.2.0
│  └─ @fortawesome/fontawesome-common-types@6.2.0
│  ├─ @fortawesome/fontawesome-common-types@6.2.1
│  └─ @fortawesome/fontawesome-common-types@6.2.1
├─ @fortawesome/react-fontawesome@0.2.0

Then type: npm update or yarn update (or update one by one). After updating, it should work as expected. Output:

yarn list | grep fontawesome
├─ @fortawesome/fontawesome-common-types@6.2.1
├─ @fortawesome/fontawesome-svg-core@6.2.1
│  └─ @fortawesome/fontawesome-common-types@6.2.1
│  └─ @fortawesome/fontawesome-common-types@6.2.1
│  └─ @fortawesome/fontawesome-common-types@6.2.1
├─ @fortawesome/react-fontawesome@0.2.0
{
  "@fortawesome/fontawesome-pro": "^6.1.2",
  "@fortawesome/fontawesome-pro-light": "^5.1.0-3",
  "@fortawesome/fontawesome-pro-solid": "^5.1.0-3",
  "@fortawesome/fontawesome-svg-core": "^6.1.2",
  "@fortawesome/free-brands-svg-icons": "^6.1.2",
  "@fortawesome/pro-duotone-svg-icons": "^6.1.2",
  "@fortawesome/pro-light-svg-icons": "^6.1.2",
  "@fortawesome/pro-regular-svg-icons": "^6.1.2",
  "@fortawesome/pro-solid-svg-icons": "^6.1.2",
  "@fortawesome/pro-thin-svg-icons": "^6.1.2",
  "@fortawesome/react-fontawesome": "^0.1.18"
}

The versions that worked for me. Bumping @fortawesome/react-fontawesome to ^0.2.0 seems to break the types.

Updating all dependencies, also the pro to the same version also helps prevent these errors.

@zacherkkila that is also what I suggested 5 above you. 😃

Not sure I saw this explicitly listed as an answer… For me the core was not the same version as the others… If this is indeed important, it would be great to have a large note added to the instructions to the website (if it isn’t already). I assume many people start with the free version and then when they add pro the versions don’t line up… No guarantee that another yarn install didn’t just fix it and the versions were irrelevant, just giving another datapoint.

"@fortawesome/fontawesome-svg-core": "^6.4.0",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/pro-solid-svg-icons": "^6.4.2",

Changing them all at the same version fixed this type error.

"@fortawesome/fontawesome-svg-core": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/pro-solid-svg-icons": "^6.4.2",

This is how you can actually use it. <FontAwesomeIcon icon={[‘fa’, ‘arrow-right-from-bracket’]} />

The first parameter ‘fa’ can be of these types export type IconPrefix = “fas” | “far” | “fal” | “fat” | “fad” | “fab” | “fak” ;

“Toxic” or no, this solved my issue. No one likes to waste hours because of confusing and wrong documentation. Not just wrong format for supplying the prop value, but also wrong example value.

With the following version & following the steps here for Next.js I don’t get any type errors.

    "@fortawesome/fontawesome-common-types": "^6.1.2",
    "@fortawesome/fontawesome-svg-core": "^6.1.2",
    "@fortawesome/free-brands-svg-icons": "^6.1.2",
    "@fortawesome/free-regular-svg-icons": "^6.1.2",
    "@fortawesome/free-solid-svg-icons": "^6.1.2",
    "@fortawesome/react-fontawesome": "^0.2.0",

Type ‘“fa-solid fa-arrow-right-from-bracket”’ is not assignable to type ‘IconProp’.

<FontAwesomeIcon icon="fa-solid fa-arrow-right-from-bracket" />

Still the same problem

import { faStar } from “@fortawesome/free-solid-svg-icons”; . . . return (<FontAwesomeIcon icon={faStar} fontSize=“50” />) . . .

This worked for me. Seems like the icon is not a string and it is of type Iconprop so you need to assign a proper object to it that can be imported from “@fortawesome/free-solid-svg-icons”; and easily use object destructive to get a specific Icon

I’m having the same issue but only for regular icons.

import React from 'react';
import { faMapMarkerAlt as regularIcon } from '@fortawesome/pro-regular-svg-icons/faMapMarkerAlt';
import { faMapMarkerAlt as solidIcon } from '@fortawesome/pro-solid-svg-icons/faMapMarkerAlt';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

export const myRegularIcon = () =>  (<FontAwesomeIcon icon={regularIcon} />); // fails
export const mySolidIcon = () =>  (<FontAwesomeIcon icon={solidIcon} />); // works

After updated to 5.14.0 bug is gone

Updating all dependencies, also the pro to the same version also helps prevent these errors.

By the way, upgrading also @fortawesome/free-brands-svg-icons sees to work

"@fortawesome/fontawesome-svg-core": "^6.3.0",
"@fortawesome/free-brands-svg-icons": "^6.3.0",
"@fortawesome/free-solid-svg-icons": "^6.3.0",

I upgraded free-solid-svg-icons today and I had this issue, I had to downgrade to the previous release in order to fix it, I think this issue should be re-opened again.

 "@fortawesome/fontawesome-svg-core": "^6.2.1",
 "@fortawesome/free-brands-svg-icons": "^6.2.1",
 "@fortawesome/free-solid-svg-icons": "^6.3.0",
 "@fortawesome/react-fontawesome": "^0.2.0",
 "@fortawesome/fontawesome-svg-core": "^6.2.1",
 "@fortawesome/free-brands-svg-icons": "^6.2.1",
 "@fortawesome/free-solid-svg-icons": "^6.2.1",
 "@fortawesome/react-fontawesome": "^0.2.0",