Typescript interface implementation has no effect on index signature

Viewed 357

For my new small project I decided to go with typescript for a reason no better than making myself feel like programming c on remarkable steroids and apparently I'm paying the price.

I have an interface, right

export default interface DataObject {
  [key: string]: any
};

Which is supposedly allowing me to define objects with string keys and whatever values. Then I'm implementing it

import DataObject from "./data-object";

export default class Model implements DataObject {

  constructor(data: DataObject = {}) {
    Model.fill(this, data);
  }

  static fill(model: Model, data: DataObject) {
    for (let prop in data) {
      model[prop] = data[prop];
    }
  }
}

Now I'm getting this error

Element implicitly has 'any' type because type 'Model' has no index signature

on this line

      model[prop] = data[prop];

But if I modify my model to include the signature

import DataObject from "./data-object";

export default class Model implements DataObject {

  [key: string]: any;

  ...
}

Then there is no error.

Why is the interface signature not having effect on my class?

1 Answers
Related