javascript - 如何在 TypeScript 中使用动态对象属性

标签 javascript typescript types interface typescript2.0

我正在使用 TypeScript,我需要处理动态对象属性。我希望我能成为 TypeScript 的大师,但我不是。我遇到的问题是,在我当前的项目中,我想让用户能够将他们自己的自定义界面输入到通用界面。

我现在得到的是如下界面

export interface Filter {
  columnId: string;
  searchTerm: string;
}
export interface GridState {
  filters: Filter[];
}

// use it as an array of filters
const state: GridState = {
  filters: [{ columnId: 'firtName', searchTerm: 'John' }]
}

但是从上面显示的内容来看,我想给用户使用他自己的界面的可能性。所以假设他想使用 field 而不是 columnId 而不是 searchTerm 他会使用 value。所以他的自定义界面将是

export interface CustomFilter {
  field: string;
  value: string;
}

我想提供给用户的是这样的东西,一个自定义的结构模板

export interface FilterStruct {
  propNameColumnId: string;
  propNameSearchTerm: string;
}

但如何将两者连接在一起?如何将用户的 CustomFilterFilterStruct 一起使用。以下内容不起作用,我知道这是不正确的。

export interface GridState {
  filters: FilterStruct<CustomFilter>[];
}

最终目标是用户能够使用他自己的界面和他自己的属性名称进行输入。然后在我这边,我将使用提供的动态对象属性循环遍历数组。

最佳答案

您可以使 GridState 通用并为通用参数提供默认值。此外,FilterStruct 可以继承 Array,因此我们可以使用辅助函数向数组添加额外的属性:

export interface FilterStruct<T> extends Array<T> {
    // We make sure the property names are actually properties of T
    // We make these optional you should use default values if they are undefined 
    // We do this to keep initialization simple in the non customized scenario, you can make them mandatory, but then you can't initialize with a simple array
    propNameColumnId?: keyof T; 
    propNameSearchTerm?: keyof T;
}
export interface Filter {
    columnId: string;
    searchTerm: string;
}
export interface CustomFilter {
    field: string;
    value: string;
}
// T has a default value of Filter so we don't have to specify it, unless we want to customize
export interface GridState<T = Filter> {
    filters: FilterStruct<T>;
}
// Helper function to create an array with the extra properties
function createFilterStruct<T>(cfg: { propNameColumnId: keyof T; propNameSearchTerm: keyof T; }, items: T[]) {
    return Object.assign(items, cfg);
}

// Default we can use simple array initailization
const state: GridState = {
    filters: [{ columnId: 'firtName', searchTerm: 'John' }]
}

// Custom filter, create with createFilterStruct
const stateCustom: GridState<CustomFilter> = {
    filters: createFilterStruct({ propNameColumnId: 'value', propNameSearchTerm: 'field' }, [
        { value: 'firtName', field: 'John' }
    ])
}

//Usage
function loopThrough<T>(grid: GridState<T>){
    // Provide defaults for propNameColumnId and propNameSearchTerm
    let propNameColumnId = grid.filters.propNameColumnId || 'columnId' as keyof T
    let propNameSearchTerm = grid.filters.propNameSearchTerm || 'searchTerm' as keyof T

    // Loop throught the array normally, it is just an array
    for(let filter of grid.filters){
        // Access the properties
        console.log(`${filter[propNameColumnId]} = ${filter[propNameSearchTerm]}`);
    }
}

loopThrough(stateCustom);
loopThrough(state);

关于javascript - 如何在 TypeScript 中使用动态对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49122204/

相关文章:

javascript - AngularJS 表单验证不起作用,显示空表单

javascript - jTinder 库不能与 AngularJS 一起正常工作

java - 使用泛型和反射时出现不兼容类型错误

c# - Visual Studio 2010 C# "already defined a member with same parameter types error."

haskell - 在 haskell 中迭代嵌套列表的新数据类型

javascript - 将 Bootstrap CSS 类应用于给定类型的所有 HTML 元素

typescript - 枚举的静态方法

javascript - map typescript 枚举

typescript - Visual Studio Code 中的 HTML KnockoutJS 智能感知

javascript - js全屏切换按钮进入全屏但不会退出