Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property is not assignable to string index in interface [duplicate]

I have the following interfaces:

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

export interface Api<T> {
  [key: string]: T[];
  meta: Meta; // error
}

Currently, I'm receiving the following error:

Property 'meta' of type 'Meta' is not assignable to string index type 'T[]'.

After searching a bit, I found this statement in TS docs:

While string index signatures are a powerful way to describe the “dictionary” pattern, they also enforce that all properties match their return type. This is because a string index declares that obj.property is also available as obj["property"].

Does it means that when I have a string index signature, I can't have any other variable without match this type?

Actually I can get rid of this error declaring the interface like this:

export interface Api<T> {
  [key: string]: any; // used any here
  meta: Meta;
}

Doing this, I lose the completely ability of type inference. Is there any way to do this without this ugly way?

like image 887
dev_054 Avatar asked Jul 22 '17 19:07

dev_054


2 Answers

You can use an intersection of two interfaces:

interface Api<T> {
    [key: string]: T[];  
}

type ApiType<T> = Api<T> & {
    meta: Meta;
}

declare let x: ApiType<string>;

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[]`
like image 185
Saravana Avatar answered Nov 02 '22 04:11

Saravana


The presented best solution didn't work when I've tried to implement this interface. I ended up nesting part with dynamic key. Maybe someone will find it useful:

interface MultichannelConfiguration {
  channels: {
    [key: string]: Configuration;
  }
  defaultChannel: string;
}
like image 40
Fifciuux Avatar answered Nov 02 '22 04:11

Fifciuux