Property is not assignable to string index in interface

前端 未结 2 1055
后悔当初
后悔当初 2021-02-05 03:05

I have the following interfaces:

export interface Meta {
  counter: number;
  limit: number;
  offset: number;
  total: number;
}

export interface Api          


        
2条回答
  •  别跟我提以往
    2021-02-05 03:09

    You can use an intersection of two interfaces:

    interface Api {
        [key: string]: T[];  
    }
    
    type ApiType = Api & {
        meta: Meta;
    }
    
    declare let x: ApiType;
    
    let a = x.meta // type of `a` is `Meta`
    let b = x["meta"]; // type of `b` is `Meta`
    
    let p = x["someotherindex"] // type of `p` is `string[]`
    let q = x.someotherindex // type of `q` is `string[]`
    

提交回复
热议问题