Typescript: bracket notation property access

后端 未结 2 1583
遥遥无期
遥遥无期 2020-12-17 09:57

I\'d like to access typed object with bracket notation like this:

interface IFoo {
    bar: string[];
}

var obj: IFoo = { bar: [\"a\", \"b\"] }
var name = \         


        
2条回答
  •  既然无缘
    2020-12-17 10:18

    I added it as a separate interface because i need to keep original..

    export interface IIndexable {
      [key: string]: any;
    }
    

    and referencing it when needed

    getEditDate(r: IRestriction, fieldName: string) {
        ...
        value={(r as IIndexable)[fieldName] as Date}
    

    works well. i will update if i find a way to shorten it

提交回复
热议问题