简体   繁体   English

索引签名参数类型必须是“字符串”或“数字”

[英]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.

相关问题 Typescript 索引签名参数类型必须是“字符串”或“数字” - Typescript An index signature parameter type must be either 'string' or 'number' Typescript 使用 keyof 时,索引签名参数类型必须是“字符串”或“数字” - Typescript An index signature parameter type must be either 'string' or 'number' when using keyof 在“string |”类型上找不到带有“string”类型参数的索引签名号码 | 环境类型' - No index signature with a parameter of type 'string' was found on type 'string | number | EnvType' 为什么索引签名参数类型不能扩展字符串或数字? - Why can't an index signature parameter type extend string or number? TypeScript:尝试使用字符串时,索引签名参数必须是“字符串”或“数字”| 数字 - TypeScript: An index signature parameter must be a 'string' or 'number' when trying to use string | number 在类型“{}”上找不到带有“字符串”类型参数的索引签名 - No index signature with a parameter of type 'string' was found on type '{}' 在类型上找不到带有“字符串”类型参数的索引签名 - No index signature with a parameter of type 'string' was found on type 在类型 ' 上找不到具有类型参数的索引签名 - No index signature with a parameter of type 'string' was found on type ' 在类型“{约会:{标签:字符串; id:字符串; minWidth:数量; }[]; }' - No index signature with a parameter of type 'string' was found on type '{ appointments: { label: string; id: string; minWidth: number; }[]; }' React 组件没有带有“字符串”类型参数的索引签名 - No index signature with a parameter of type 'string' for a React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM