簡體   English   中英

在 Typescript 接口中訪問索引

[英]Accessing index in Typescript Interface

我使用索引類型在 Typescript 中創建了一個字典,我正試圖在我的代碼中的某個地方使用它。 但無法弄清楚如何訪問索引。 我有兩個接口:

export interface OrderForm {
 model_id?: number;
 model_color_id?: number;
 model_product_id?: number;
 total_lld: ComputedTotalLLDOptions;
 product_parts_id?: string;
}

export interface ComputedTotalLLDOptions {
  [index: number]: {
    contribution: number;
    duration: number;
    rent: number;
  };
}

你有一些我如何使用它的例子嗎?

接口定義了代碼中“事物”的形狀; 它們提供 static 類型並且沒有運行時功能。 您可以使用它們來指定變量/參數/屬性的預期類型。 您的ComputedTotalLLDOptions示例:

const foo: ComputedTotalLLDOptions = {
    0: {
        contribution: 1,
        duration: 2,
        rent: 3
    }
}

const bar: number = foo[0].contribution

看到這個游樂場

暫無
暫無

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

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