Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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 {   constructor(     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 this.dataStore.data.find((element) => {   if (element.id === conversationId) {     element.isUnread = true;     // Push the updated list of conversations into the observable stream     this.observer.next(this.dataStore.data);   } }); 

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

like image 974
be-codified Avatar asked Mar 09 '16 15:03

be-codified


People also ask

Is not assignable to type void TypeScript?

The "Type 'void' is not assignable to type" TypeScript error occurs when we forget to return a value from a function, so the function gets an implicit return type of void . To solve the error, make sure you return a value of the correct type from your functions before the assignment.

What does () => void mean TypeScript?

Introduction to TypeScript void type The void type denotes the absence of having any type at all. It is a little like the opposite of the any type. Typically, you use the void type as the return type of functions that do not return a value.

Is not assignable to type Boolean undefined?

The "Type 'boolean | undefined' is not assignable to type boolean" error occurs when a possibly undefined value is assigned to something that expects a boolean . To solve the error, use the non-null assertion operator or a type guard to verify the value is a boolean before the assignment.

Is not assignable to type string?

The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion.


2 Answers

It means that the callback function you passed to this.dataStore.data.find 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:

this.dataStore.data.find((element, index, obj) => {     // ...      return true; // or false }); 

or:

this.dataStore.data.find(element => {     // ...      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.

like image 78
John Weisz Avatar answered Sep 19 '22 11:09

John Weisz


Your code is passing a function as an argument to find. That function takes an element argument (of type Conversation) and returns void (meaning there is no return value). TypeScript describes this as (element: Conversation) => void'

What TypeScript is saying is that the find function doesn't expect to receive a function that takes a Conversation and returns void. It expects a function that takes a Conversations, a number and a Conversation array, and that this function should return a boolean.

So bottom line is that you either need to change your code to pass in the values to find correctly, or else you need to provide an overload to the definition of find in your definition file that accepts a Conversation and returns void.

like image 20
NYCdotNet Avatar answered Sep 22 '22 11:09

NYCdotNet