Property is not assignable to string index in interface

Viewed 27310

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?

2 Answers

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;
}
Related