#Object is possibly undefined
13 messages · Page 1 of 1 (latest)
im not sure if using the proper narrowing would do anything though, you'd maybe have to rewrite it a bit i suppose
Unfortunately, only the in operator narrows 😦
well that confirms my suspicion
i guess you'd have to do something like this
if (tag in acc) {
acc[tag].push(transaction);
} else {
acc[tag] = [transaction];
}
TS can't track mutations
makes sense
does this not work?
I believe this works without a cast:
let transactions = acc[tag];
if(!transactions) {
transactions = acc[tag] = [];
}
transactions.push(transaction);
@coarse delta Here's a shortened URL of your playground link! You can remove the full link from your message.
Preview:```ts
type Transaction = {
id: string
amount: number
tags: string[]
}
const transactions: Transaction[] = [
{
id: "1",
amount: 100,
tags: ["123", "456", "789"],
},
]
const transactionsGroupedByTag = transactions.reduce(
(acc, transaction) => {
transaction.tags.forEach(tag
...```
@coarse delta Here's a shortened URL of your playground link! You can remove the full link from your message.
Preview:```ts
type Transaction = {
id: string
amount: number
tags: string[]
}
const transactions: Transaction[] = [
{
id: "1",
amount: 100,
tags: ["123", "456", "789"],
},
]
const transactionsGroupedByTag = transactions.reduce(
(acc, transaction) => {
transactio
...```