簡體   English   中英

ReactJs / Typescript如何將對象數組傳遞給接口道具?

[英]ReactJs / Typescript How to pass an array of objects into interface props?

我有兩個接口,看起來像這樣。

export interface TableBoxPropsHeader{
    name: string,
    isIconOnly: boolean
}

export interface TableBoxProps<T> {
 headerNames: TableBoxPropsHeader[],
 // some stuff
}

我試圖用這個定義創建一個變量,但由於某種原因它說我試圖傳遞一個string[]數組而不是TableBoxPropsHeader[]數組。

private tableBoxProps: TableBoxProps<SomeType> = {
  headerNames: [{name: "Name", isIconOnly:false}, {name: "Category", isIconOnly:true}],

我正在使用VSCode並且它沒有抱怨上述內容。 但是npm打印出以下內容

Types of property 'headerNames' are incompatible.
  Type 'string[]' is not assignable to type 'TableBoxPropsHeader[]'
    Type 'string' is not assignable to type 'TableBoxPropsHeader'.

我究竟做錯了什么? 如何創建接口數組?

好吧,這對我來說有些愚蠢。 錯誤中的另一個文件中有另一個變量。 它現在完美無缺......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM