Imagine we have interface below:
interface Interface1 {
[index: string]: string;
param1: string;
}
And we would like to implement above interface; also we want to add another field with the type of number
to the class fields:
class MyClass implements Interface1 {
[index: string]: string;
param1: string;
param2: number; // TS2411 error
}
But we get an error, as our param2
type does not match the type of the index signature, so instead I tried to change index signature (I was expecting it to not solve the problem):
class MyClass implements Interface1 { // TS2420
[index: string]: string | number;
param1: string;
param2: number;
}
So, my question is how do you implement interface (without modifying it) with index signature and add another field with different type in class?
interface SomeInterface {
param: string;
}
class SomeClass implements SomeInterface {
param: string;
num: number;
}
How do you do same for interface that has index signature?