类型 '() => string' 不能分配给类型 'string' Typescript React



我经常看到这个问题,但没有一个答案对我有效。最接近的答案在这里,但也不起作用。

我有像这个一样的Tsx代码

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

以及功能

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

我得到错误类型'((=>string'不可分配给类型"string">我不知道如何解决这个问题。常见的答案是键入这样的函数。

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

然后调用类似的函数

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

但是,虽然这很有效,但react并不喜欢你这样做,然后它迫使我使用像这样的匿名函数

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

返回原始问题

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

应该是这样的

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

相关内容

最新更新