Home > other >  Type '() => string' is not assignable to type 'string' Typescript React
Type '() => string' is not assignable to type 'string' Typescript React

Time:10-19

I have seen this question a lot but none of the answers really work for me. Closest answer was here but that also doesn't work.

I have Tsx code like this

<img src={getLogo} alt="Airline Logo" />

and the function

const getLogo: string = () => {
  return "";
};

I get the error Type '() => string' is not assignable to type 'string' I don't know how to fix that. Common answer was to type the function something like this.

const getLogo:() => string = () => {
  return "";
};

then call the function like this

<img src={getLogo()} alt="Airline Logo" />

But whilst this works react doesn't really like when you do that and then it forces me to use an anonymous function like this

<img src={() => getLogo()} alt="Airline Logo" />

Which returns the original problem

'() => string' is not assignable to type 'string'

CodePudding user response:

It should be like this

const getLogo = (): string => {
  return "";
};

CodePudding user response:

const getLogo: string = () => {
  return "";
};
 
const logo = getLogo();

... ....

<img src={logo} alt="Airline Logo" />
  • Related