[英]An index signature parameter type must be either 'string' or 'number'
I building an E-shop in React with typescript where I have a Cart and a CartItem class as shown below:我使用打字稿在 React 中构建了一个电子商店,其中有一个 Cart 和一个 CartItem 类,如下所示:
export class CartItem {
public id: bigint;
public file: File;
public constructor(file: File) {
this.id = file.id;
this.file = file;
}
}
export class Cart{
public items: { [id: bigint]: CartItem };
public constructor() {
this.items = {};
}
}
The issue is that An index signature parameter type must be either 'string' or 'number'
Right now its necessary for id to be bigint which isn't compatible as an index signature parameter.问题是
An index signature parameter type must be either 'string' or 'number'
现在必须将 id 设为 bigint,这与索引签名参数不兼容。 Any suggestions on how to solve this?有关如何解决此问题的任何建议?
You can simply cast your bigint to a string.您可以简单地将 bigint 转换为字符串。
Alternatively, you can also take the hash of the bigint.或者,您也可以获取 bigint 的哈希值。 This will give you a set length for your string and might work better if you work with truly giant numbers.
这将为您的字符串提供一个固定的长度,如果您使用真正的巨大数字,它可能会更好地工作。
If you need the original id, just add it to the basketitem class if you're using the hash aproach.如果您需要原始 id,如果您使用哈希方法,只需将其添加到篮项类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.