簡體   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