I am generating object with time intervals with some func, it returns
["00:00","00:30","01:00"]
... , but for my purpose i need to have obj-map
{{"00:00": "00:00"}, {"00:30":"00:30"}, {"01:00":"01:00"}}
I have trouble typing the reduce func below, how can i type acc or the return value of the func to avoid using any
as type for acc
?
generateTimeIntervals(0, 1440, 30).reduce((acc, val) => {
acc[val] = val;
return acc;
}, {})
You can pass in the type as an argument to reduce
:
let result = generateTimeIntervals(0, 1440, 30).reduce<Record<string, string>>((acc, val) => {
acc[val] = val;
return acc;
}, {})
See an example here: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABAcwKZlQJwIZVQFRgFtUBJMPTAN2wBsBnACmwC4wQiAjLAGkU5aJ2XXogiDh3TAEpEAbwCwAKESJMqKCExIA2stWqAylEwwwyZtL7HT5xpyuIbZixGn7EAXWUBfZctoNNVR6EFooRABeFHQsXAJiMgosGgZGAAY+AEYAFhzMxABmdOkAOnUAExAIVAAeACVUCDhMCtr6Exc+DttkAD4+xmYICD5U2Ui++Q9sEZ1UzyjEVIBuD3VNbURZiDWlHz45H3clZWawejhA0to4CwAidVDw+74nsKhpIA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With