[英]How to set the Type of a function in Typescript, when passing that into another function?
Here is my function: 这是我的功能:
const payout = (asset: ILPAsset, type: string)
=> type === 'daily' ? asset.lastPayout : asset.historical;
And where I'm using it: 而我在哪里使用它:
@bind
private mapDailyAssets(payoutType: string, payout: payoutFunc, assets: ILPAsset[], currency: string) {
return assets.map((asset) => (
<div className={b('table-row')()} key={asset.symbol}>
<div>{asset.symbol}</div>
<div className={b('asset-value')()}>{formatMoney(payout(asset, payoutType), currency)}</div>
</div>
));
}
I'm getting errors when trying to set an interface
for type payoutFunc
: 尝试为类型
payoutFunc
设置interface
时出现错误:
interface payoutFunc: (asset: ILPAsset, type: string) => string;
But also getting this error: 但是也得到这个错误:
invoke an expression whose type lacks a call signature
调用其类型缺少调用签名的表达式
Your syntax for declaring a function signature interface is not quite right. 您用于声明功能签名接口的语法不太正确。 It should look like this:
它看起来应该像这样:
interface payoutFunc {
(asset: ILPAsset, type: string): string;
}
Or you could use a type alias : 或者您可以使用类型别名 :
type payoutFunc = (asset: ILPAsset, type: string) => string;
In either case you can use this type as a prop somewhere else: 无论哪种情况,您都可以在其他地方将此类型用作道具:
interface MyProps {
foo: string;
bar: number;
payout: payoutFunc;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.