我的职责是:
const payout = (asset: ILPAsset, type: string) => type === 'daily' ? asset.lastPayout : asset.historical;
在我使用它的地方:
@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> )); }
interface 对于类型 payoutFunc :
interface
payoutFunc
interface payoutFunc: (asset: ILPAsset, type: string) => string;
但也得到了这个错误:
声明函数签名接口的语法不太正确。应该是这样的:
interface payoutFunc { (asset: ILPAsset, type: string): string; }
或者你可以用一个 type alias :
type payoutFunc = (asset: ILPAsset, type: string) => string;
无论哪种情况,您都可以在其他地方使用此类型作为道具:
interface MyProps { foo: string; bar: number; payout: payoutFunc; }