简体   繁体   中英

Getting object property

I am new to TypeScript.

How to fix this:

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

Error:

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

Another Problem:

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

Error 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 '{}'.

You can do one of two things:

  1. Implement a strongly-type definition for your layout object with an interface:
interface ILayout {
    [index: string]: string[] | number, // And any other types which your object returns
    numRowsToDraw: number
}

var layout = {
    numRowsToDraw: 10
};

And then explicitly cast your accessed property to an array:

(<string[]>layout[self._swapEntries ? '_rows' : 'rows']).slice(0, layout.numRowsToDraw);
  1. Use the --suppressImplicitAnyIndexErrors flag to tell the compiler that you wish to be allowed to access object keys using a type not known at compile-time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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