typescript interface with key index generates error

Viewed 17
1 Answers

You still have to declare properties that are provided by implements.

For example:

interface A { a: string }

class Foo implements A {
  constructor() { this.a = 'abc' }
}
// Class 'Foo' incorrectly implements interface 'A'.
//   Property 'a' is missing in type 'Foo' but required in type 'A'.(2420)

class Bar implements A {
  a: string
  constructor() { this.a = 'abc' }
}
// Fine

That means you need to declare the index signature in your class as well.

class O implements I0 {
  [key: string]: I1

  m: {
    p1: "test",
    p2: "test"
  }
}

Here implements checks to make sure that the class supports interface I0, but that's all implements actually does.

See Playground

Related