TypeScript 错误:类型“void”不能分配给“boolean”类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35896064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:59:27  来源:igfitidea点击:

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

typescript

提问by be-codified

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

'(element: Conversation) => void' 类型的参数不可分配给类型 '(value: Conversations, index: number, obj: Conversation[]) => boolean' 的参数。“void”类型不能分配给“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.

这个错误是什么意思?提前谢谢你。

回答by John Weisz

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

这意味着您传递给的回调函数this.dataStore.data.find应该返回一个布尔值并有 3 个参数,其中两个可以是可选的:

  • 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:

但是,您的回调函数不返回任何内容(返回 void)。您应该传递一个具有正确返回值的回调函数:

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 findmethod is called a predicate. The predicate here defines a boolean outcome based on conditions defined in the function itself, so that the findmethod can determine which value to find.

这样做的原因:传递给find方法的函数称为predicate。此处的谓词根据函数本身中定义的条件定义了一个布尔结果,以便该find方法可以确定要查找的值。

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

实际上,这意味着为 中的每个项目调用谓词data,并且data您的谓词返回的第一个项目是返回true的值find

回答by NYCdotNet

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

您的代码将一个函数作为参数传递给find. 该函数接受一个element参数(类型Conversation)并返回void(意味着没有返回值)。TypeScript 将其描述为(element: Conversation) => void'

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

TypeScript 的意思是该find函数不期望接收一个接受 Conversation 并返回 void 的函数。它需要一个接受 a Conversations、 anumber和一个Conversation数组的函数,并且这个函数应该返回 a boolean

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

所以最重要的是,您要么需要更改代码以find正确传递值,要么需要find在定义文件中为接受 aConversation并返回的定义提供重载void