I have the following interface / type as shown below:
export interface Bar {
title?: string;
[key: string]: string;
}
export type Foo = {
name: string;
} & {
[key: string]: Bar;
};
And when I try to declare a variable like so:
const someObject: Foo = {
name: 'John',
city: {
a: 'something',
},
};
It gives me the error:
Type '{ name: string; city: { a: string; }; }' is not assignable to type 'Foo '. Type '{ name: string; city: { a: string; }; }' is not assignable to type '{ [key: string]: Bar ; }'. Property 'title' is incompatible with index signature. Type 'string' is not assignable to type 'Bar'.
Question:
How to create an interface to satisfy an object like:
const someObject: Foo = {
name: 'John',
randomProperty1: { a: 'a' },
randomProperty2: { b: 'b' },
// any number of 'Bar'
};