Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typing Object.values(users).forEach

Tags:

typescript

I was wondering how to fix this code, if I change the type of room to string on forEach((room: string) I got this error:

Argument of type '(room: string) => void' is not assignable to parameter of type '(value: unknown, index: number, array: unknown[]) => void'. Types of parameters 'room' and 'value' are incompatible. Type 'unknown' is not assignable to type 'string'.

Otherwise if I leave blank, I had to convert room as string on the line batch.update.

Object.values(users).forEach((room: string) => {
  batch.set(firestore.collection('messages').doc(), {
    ...message,
    room,
  })

  batch.update(firestore.collection('rooms').doc(room), { // if I leave room as `unknow` I have an error here.
     ...
  })
})

What is the correctly way to fix the typing?

like image 731
Rodrigo Avatar asked Oct 29 '25 03:10

Rodrigo


1 Answers

Simply declared the callback argument type should just be fine.

Object.values<string>(users).forEach(s => /* ... */)
like image 93
equt Avatar answered Oct 31 '25 22:10

equt