3 ms·
The type annotation gymnastics you sometimes have to do when reducing to an object in TypeScript are annoying. allTasks.reduce((acc, item) => { acc[item.labe
by flufluflufluffy 17d ago
The type annotation gymnastics you sometimes have to do when reducing to an object in TypeScript are annoying.
allTasks.reduce((acc, item) => {
acc[item.label] = t => t.item.label === item.label;
return acc;
}, {} as Record<string, (t: typeof tasks[number]) => boolean>)
- LittleLily 17d agoI agree it's a bit annoying, but a better solution than using `as` is just telling reduce what its generic type should be: tasks.reduce<Record<string, (t: typeof tasks[number]) => boolean>>((acc, item) => ..., {}) Also imo it's cleaner to reduce to an object with something like this as the callback: (acc, item) => ({ ...acc, [item.label]: t => t.label === item.label })
- flufluflufluffy 16d agooh man I somehow never realized I could just use that square bracket syntax. must update everything