be-codified be-codified - 1 year ago 132
TypeScript Question

TypeScript error: Type 'void' is not assignable to type 'boolean'

I am having a TypeScript error:

Argument of type '(element: Conversation) => void' is not assignable to parameter of type '(value: Conversations, index: number, obj: Conversation[]) => boolean'. Type 'void' is not assignable to type 'boolean'.

This is my schema

export class Conversation {
public id: number,
public dateTime: Date,
public image: string,
public isUnread: boolean,
public title: string
) {}

and this is my code

// Mark as read also in data store => {
if ( === conversationId) {
element.isUnread = true;
// Push the updated list of conversations into the observable stream;

What does this error means? Thank in you in advance.

Answer Source

It means that the callback function you passed to should return a boolean and have 3 parameters, two of which can be optional:

  • value: Conversations
  • index: number
  • obj: Conversation[]

However, your callback function does not return anything (returns void). You should pass a callback function with the correct return value:, index, obj) => {
    // ...

    return true; // or false

or: => {
    // ...

    return true; // or false

Reason why it's this way: the function you pass to the find method is called a predicate. The predicate here defines a boolean outcome based on conditions defined in the function itself, so that the find method can determine which value to find.

In practice, this means that the predicate is called for each item in data, and the first item in data for which your predicate returns true is the value returned by find.