[英]Issue in specify type for object in typescript
I have an set of data for which I need to specify type in typescript (type any is not recommended).我有一组数据,需要在 typescript 中指定类型(不推荐类型 any)。
'xyzzy': (2) [Array(4), 1],
I get error when i specify its type as object. Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.当我将其类型指定为 object 时出现错误。元素隐式具有“任何”类型,因为类型“字符串”的表达式不能用于索引类型“{}”。 No index signature with a parameter of type 'string' was found on type '{}'.
在类型“{}”上找不到参数类型为“字符串”的索引签名。
You can specify new type with type
keyword;您可以使用
type
关键字指定新类型;
type MyType = {
xyzzy: [number[], number],
abs: [number[], number]
}
And then use this declaration然后使用这个声明
const something: MyType = {
xyzzy: [[0, 1, 2, 3], 1],
abs: [[0, 1, 2, 3], 0],
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.