繁体   English   中英

获取对象属性

[英]Getting object property

我是TypeScript的新手。

如何解决这个问题:

layout[self._swapEntries ? '_rows' : 'rows'].slice(0, layout.numRowsToDraw);

错误:

error TS7017: Index signature of object type implicitly has an 'any' type.

另一个问题:

 let entries = rows.selectAll("g." + Legend.LEGEND_ENTRY_CLASS).data((d) => d);

错误2:

error TS2345: Argument of type '(d: {}) => {}' is not assignable to parameter of type '(datum: {}, index: number, outerIndex: number) => {}[]'.
Type '{}' is not assignable to type '{}[]'.
Property 'length' is missing in type '{}'.

您可以执行以下两项操作之一:

  1. 使用接口为layout对象实现强类型定义:
interface ILayout {
    [index: string]: string[] | number, // And any other types which your object returns
    numRowsToDraw: number
}

var layout = {
    numRowsToDraw: 10
};

然后将访问的属性显式转换为数组:

(<string[]>layout[self._swapEntries ? '_rows' : 'rows']).slice(0, layout.numRowsToDraw);
  1. 使用--suppressImplicitAnyIndexErrors标志来告知编译器希望允许您使用编译时未知的类型来访问对象键

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM