简体   繁体   English

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

[英]Typescript An index signature parameter type must be either 'string' or 'number'

I have a generic which its default type is a string :我有一个泛型,它的默认类型是一个string

interface EntityState<typeOfID = string> {
  entities: { [ id: typeOfID]: any };
}

I get the error:我得到错误:

An index signature parameter type must be either 'string' or 'number'.(1023)

I also tried the following:我还尝试了以下方法:

interface EntityState<typeOfID extends string | number> {
  entities: { [ id: typeOfID]: any };
}

And it doesn't work.它不起作用。 How to solve it?如何解决?

You can use Record in such a case.在这种情况下,您可以使用Record

Consider this:考虑一下:

export const enum MyEnumKeys {
  Key1 = 'key1',
}

interface EntityState<typeOfID> {
  entities: Record<typeOfID, any>;
}

const test: EntityState<MyEnumKeys> = {
  entities: {
    key1: 1,
    anotherKey: 2 // error here
  }
}

In JavaScript (and as such, TypeScript), object keys are always string or Symbol This is what TypeScript is trying to tell you: You cannot set the type of a key to anything other than these.在 JavaScript(以及 TypeScript)中,object 键始终是字符串或符号这就是 TypeScript 试图告诉您的任何内容:您不能设置其他类型的键。

From Mozilla's Developer Docs: 来自 Mozilla 的开发者文档:

Please note that all keys in the square bracket notation are converted to string unless they're Symbols, since JavaScript object property names (keys) can only be strings or Symbols (at some point, private names will also be added as the class fields proposal progresses, but you won't use them with [] form).请注意,方括号表示法中的所有键都将转换为字符串,除非它们是符号,因为 JavaScript object 属性名称(键)只能是字符串或符号(在某些时候,私有名称也将添加为 ZA2F2ED4F8EBC2B61DZC4 提案进展,但您不会将它们与 [] 形式一起使用)。 For example, in the above code, when the key obj is added to the myObj, JavaScript will call the obj.toString() method, and use this result string as the new key.例如,在上面的代码中,当 key obj 添加到 myObj 中时,JavaScript 会调用 obj.toString() 方法,并将这个结果字符串作为新的 key。

In your first example, you're saying the key type can be pretty much anything, but it's usually strings.在您的第一个示例中,您说键类型几乎可以是任何东西,但通常是字符串。 This allows ie booleans, which is not allowed.这允许 ie 布尔值,这是不允许的。

In your second example, you are saying that the key type is a superset of string and numbers.在您的第二个示例中,您说键类型是字符串和数字的超集。 Which is still not allowed, as the types can still not guarantee that they are stringify-able.这仍然是不允许的,因为这些类型仍然不能保证它们是可字符串化的。 (ie null, which conveniently has typeof(null) == "object", but cannot be converted to string.) (即null,方便有typeof(null) == "object",但不能转换成字符串。)

Edit: Numbers are also allowed , as they are guaranteed to have be able to convert to string.编辑: 数字也是允许的,因为它们保证能够转换为字符串。 The correct term for "stringify-able" is indexable. “stringify-able”的正确术语是可索引的。

Edit 2: Although JavaScript allows for Symbol as keys, TypeScript currently do not.编辑 2:虽然 JavaScript 允许符号作为键,但 TypeScript 目前不允许。 You can read the discussion which started in 2015 at TypeScript's GitHub Repository您可以在TypeScript 的 GitHub 存储库中阅读 2015 年开始的讨论

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 使用 keyof 时,索引签名参数类型必须是“字符串”或“数字” - Typescript An index signature parameter type must be either 'string' or 'number' when using keyof 索引签名参数类型必须是“字符串”或“数字” - An index signature parameter type must be either 'string' or 'number' TypeScript:尝试使用字符串时,索引签名参数必须是“字符串”或“数字”| 数字 - TypeScript: An index signature parameter must be a 'string' or 'number' when trying to use string | number 打字稿 - 没有带有“字符串”类型参数的索引签名 - Typescript - No index signature with a parameter of type 'string' Typescript: 在类型 '{ "A": string; 上找不到参数类型为 'string' 的索引签名 } - Typescript: No index signature with a parameter of type 'string' was found on type '{ "A": string; } 在“{}”类型上找不到具有“数字”类型参数的索引签名 - Typescript 编译器错误 - No index signature with a parameter of type 'number' was found on type '{}' - Typescript compiler error Typescript 错误未在类型上找到参数类型为“string”的索引签名 - Typescript error No index signature with a parameter of type 'string' was found on type 反应 Typescript 在类型上找不到参数类型为“string”的索引签名 - React Typescript No index signature with a parameter of type 'string' was found on type 打字稿:在类型“{}”上找不到带有“字符串”类型参数的索引签名 - Typescript: No index signature with a parameter of type 'string' was found on type '{}' 打字稿错误:在类型“{}”上找不到带有“字符串”类型参数的索引签名 - Typescript error : No index signature with a parameter of type 'string' was found on type '{}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM