Message from JavaScript discussions

November 2020

— I believe I wanted to do something similar lately.. 1 sec. Maybe you have a proper solution

— 

I wanted to count type of events per device (Error, Warning, etc)
However the types are dynamic. So I cannot simply do <Record ... >

— Sure you can

— Record<string, foo>

— Dynamic keys

— But uhh, *how* dynamic?

— 

/**
*
* param {number} id
* returns {Promise<Record<'Error' | 'Warning' | 'Notificiation' | 'Message' | 'Unknown', number>>}
*/
async countTypesByDevice(id) {
const types = await bookshelf.model('EventType').fetchAll()
const typeNames = R.map(R.prop('Type'), types.toJSON())
const typeCounts = await Promise.all(R.map(async type => ({
type,
count: await this.where({ type, id }).count()
}), typeNames))
// ts-ignore
return R.reduce((acc, cur) => R.assoc(cur.type, cur.count, acc), {}, typeCounts)
}

Message permanent page

— If you can find all instances where you construct an event, you can make them static

— Oof

— Just TS it man

— We are still converting.

— Or.. I am converting.