反应打字稿错误 - 元素隐式具有任何类型,因为字符串类型的表达式不能用于索引类型 {}
我正在尝试使用打字稿在我的反应项目中按 x 对数组进行分组,并收到以下错误:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
No index signature with a parameter of type 'string' was found on type '{}'. TS7053
有人可以帮我定义这个,以便它消除打字稿错误吗?我对 React 完全陌生。
这是错误的位置
const result = trades.reduce((groupedAccounts, trade) => {
const account = trade.pacct;
if (groupedAccounts[account] == null) groupedAccounts[account] = [];
groupedAccounts[account].push(trade);
return groupedAccounts;
}, {});
,这是我从另一个 .ts 导出的接口,
export interface TradeData {
id: number;
filedate: Date;
poffic: string;
pacct: string;
quantity: number;
sector: string;
psdsC1: string;
name: string;
bbsymbol: string;
last_price: number;
deltasettlement: number;
}
我确信这是我不明白的语法问题。
I am trying to group an array by x in my react project using typescript and getting the following error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
No index signature with a parameter of type 'string' was found on type '{}'. TS7053
Can someone help me define this so it removes the typescript error? I am completely new to React.
here is the location of the error
const result = trades.reduce((groupedAccounts, trade) => {
const account = trade.pacct;
if (groupedAccounts[account] == null) groupedAccounts[account] = [];
groupedAccounts[account].push(trade);
return groupedAccounts;
}, {});
here is my export interface from another .ts
export interface TradeData {
id: number;
filedate: Date;
poffic: string;
pacct: string;
quantity: number;
sector: string;
psdsC1: string;
name: string;
bbsymbol: string;
last_price: number;
deltasettlement: number;
}
I'm sure its a syntax issue I just don't understand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
降低
可以采用通用来指定谓词返回的内容(以及初始值的类型)。在这里,它被推断为
{}
,因为初始值:您可能希望它为
record< strine,tradeData []>
:reduce
can take a generic to specify what the predicate returns (and the type of the initial value).Here it is inferred as
{}
because of the initial value:You probably want it to be
Record<string, TradeData[]>
: