[英]How do I translate this map function from Javascript to Typescript given the structure of the data object being mapped?
I'm translating an app from Javascript to Typescript and I have come upon a type declaration situation I do not know how to resolve.我正在将一个应用程序从 Javascript 翻译为 Typescript,我遇到了类型声明情况,我不知道如何解决。 I barely understand how I got this to work to begin with in JS:我几乎不明白我是如何在 JS 中开始工作的:
{data.listing.map(({ token: { name, display_uri }, price, amount_left }, index, arr) => (
<Col key={index} id={index} xs={4} sm={4} md={3} lg={2}>
{display_uri ? <StateImg src={"https://ipfs.io/ipfs/" + display_uri.slice(7,)}/>: null}
<h5>name: {name}</h5>
<p>price: {price / 1000000}</p>
<Row>
<QuantityForm
value={batch.find((b) => b.index === index)?.count || 0}
maxValue={amount_left}
onChange={(v) => onChange(index, v, arr[index], target)}
/>
</Row>
</Col>
))}
How do I format the param declarations correctly given the following types:给定以下类型,如何正确格式化参数声明:
name = string,
display_uri = string,
price = number,
amount_left = number,
index = number,
arr = Object[] (not sure if this one is correct; it's the 3rd map param, so it's the same array being mapped)
Kind thanks for any help.非常感谢您的帮助。 It is greatly appreciated.非常感谢。
You're better off defining the type of data
, but without further code or context, you can use as
to cast data.listing
to the correct type:您最好定义data
的类型,但无需进一步的代码或上下文,您可以使用as
将data.listing
转换为正确的类型:
(data.listing as {
token: { name: string; display_uri: string; };
price: number;
amount_left: number;
}[]).map(({ token: { name, display_uri }, price, amount_left }, index, arr) =>
You might want to try the officialTypeScript handbook before using TypeScript to get a feel for it.在使用 TypeScript 之前,您可能想尝试一下官方的TypeScript 手册来感受一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.