I want to convert a Typescript Map to an object. The following code shows a compiler error:
const sourceMap = new Map<string, string>();
sourceMap.set('foo', 'bar');
const jsonObject = {};
sourceMap.forEach((value, key) => {
jsonObject[key] = value;
});
console.log(JSON.stringify(jsonObject));
The compiler doesn't like the line jsonObject[key] = value; saying:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'. No index signature with a parameter of type 'string' was found on type '{}'.(7053)
How can I fix that or what am I doing wrong here?