I have the following Typescript code which I'm trying to fit the type Data with at least a mainData property.
interface Data {
[K: string]: string
mainData: string
}
function getDataObj(dynamicData: string[]): Data {
return dynamicData.reduce((res, val) => ({
...res,
[val]: 'some stuff'
}), {})
}
// In the actual app, the dynamicData are coming dynamically and are not hardcoded like so in this example
const dynamicData = ['dataA', 'dataB', 'mainData']
const dataObj: Data = getDataObj(dynamicData)
However, the code above will give me the following error:
Property 'mainData' is missing in type '{}' but required in type 'Data'.(2741)
input.ts(3, 5): 'mainData' is declared here
The problem is that Typescript doesn't trust that mainData will definitely exist in the dynamicData parameter array. However, in my case, the mainData property will definitely exist on the array but I just want to add it to the Data interface for better code completion.
Here's the snippet on TS Playground.
What else can I do so that Typescript will trust that I will have mainData created as one the properties in the Data object?