Consider this code:
interface MyInterface {
foo: string
bar: string
baz: string
}
const myObj: MyInterface = {
foo: "foo",
bar: "bar",
baz: "baz"
};
Object.keys(myObj).forEach(obj => {
obj = myObj[obj];
});
When enabling strict mode I get this error: TS7017: Element implicitly has an 'any' type because type 'MyInterface' has no index signature.
The easiest solution seems to be:
interface MyInterface {
[key: string]: string;
foo: string
bar: string
baz: string
}
however this opens up for any string properties in MyInterface-objects.
Then I was thinking of using a mapped type instead:
type ValidEnteries = "foo" | "bar" | "baz";
type Alternative = {
[key in ValidEnteries]: string
}
While this seems correct to me, the original problem returns with missing index signature.
Is there any way to both have an index signature, and limit the object to a certain number of properties?