#Object is possibly undefined

13 messages · Page 1 of 1 (latest)

distant mountain
#

hasOwnProperty doesn't do anything in the type system

#

im not sure if using the proper narrowing would do anything though, you'd maybe have to rewrite it a bit i suppose

clever kayak
#

Unfortunately, only the in operator narrows 😦

distant mountain
#

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];
}
clever kayak
#

TS can't track mutations

distant mountain
#

makes sense

broken slate
#

I believe this works without a cast:

let transactions = acc[tag];
if(!transactions) {
    transactions = acc[tag] = [];
}
transactions.push(transaction);
pearl pagodaBOT
#

@coarse delta Here's a shortened URL of your playground link! You can remove the full link from your message.

cmpsb#2699

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
...```

long spade
#

tbh, that reduce + mutating theoriginal object isn't ideal

#

yeah, 100%

pearl pagodaBOT
#

@coarse delta Here's a shortened URL of your playground link! You can remove the full link from your message.

cmpsb#2699

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
...```