To better understand this concept, let's take a closer look at the code snippet provided:
interface FooInterface {
bar: (flags: { [key: string]: string }) => void;
}
export class Foo implements FooInterface {
bar(flags: { myFlag: string }) {}
}
In implementing FooInterface.bar
, I require that the parameter be an object, regardless of its keys.
However, when I implemented this in the Foo
class with the key named as myFlag
, it resulted in an error stating that this particular key does not exist within the interface. The full error message is shown below.
How can I instruct Typescript to overlook the specific keys used in the implementation classes?
The error received:
src/extensions/test/test.provider.ts:24:3 - error TS2416: Property 'bar' in type 'Foo' cannot be assigned to the same property in base type 'FooInterface'.
Type '(flags: { myFlag: string; }) => void' is not compatible with type '(flags: { [key: string]: string; }) => void'.
The parameters 'flags' are incompatible.
Property 'myFlag' is required in type '{ myFlag: string; }', but missing in type '{ [key: string]: string; }'.
24 bar(flags: { myFlag: string }) {}
~~~