简体   繁体   English

打字稿修复错误类型的参数不可分配

[英]typescript fix error argument of type is not assignable

I don't know why I'm facing a problem.我不知道为什么我遇到了问题。 I have a function that takes an array of objects.我有一个接受对象数组的函数。 Every object has a property named id and I wanted to get the last id from the given data.每个对象都有一个名为id的属性,我想从给定的数据中获取最后一个id The code is working fine.代码工作正常。

type Data = object;

const data = [
    {
        'id': 1,
        'name': 'Uncategorized',
    },
    {
        'id': 2,
        'name': 'DSLR Cameras',
    },
    {
        'id': 3,
        'name': 'Printer / Ink',
    },
];

const getLastId = (data: Data[]): number => {
    if (Array.isArray(data) && data.length > 0) {
        // Create an array of Id's of all items
        const idsArray: number[] = [];
        data.forEach((obj: { id: number }) => idsArray.push(obj.id));
        // Return last element id
        return idsArray[data.length - 1];
    } else {
        return 1;
    }
};

But, at the following line, an error is coming.但是,在以下行中,将出现错误。

data.forEach((obj: { id: number }) => idsArray.push(obj.id)); data.forEach((obj: { id: number }) => idsArray.push(obj.id));

TS2345: Argument of type '(obj: { id: number; }) => number' is not assignable to parameter of type '(value: object, index: number, array: object[]) => void'.
  Types of parameters 'obj' and 'value' are incompatible.
    Property 'id' is missing in type '{}' but required in type '{ id: number; }'.

What type of error it is and How to fix this?这是什么类型的错误以及如何解决这个问题? Link 关联

Your Data (I do not like this name) declaration is Object, but should be like this:你的数据(我不喜欢这个名字)声明是对象,但应该是这样的:

type Data = {
    id: number,
    name: string,
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VSCode 如何修复 typescript 突出显示错误 {} 类型的参数不可分配给 - VSCode how to fix typescript highlight error Argument of type {} is not assignable to parameter of Type的参数在Typescript中不可分配错误 - Argument of type is not assignable error in Typescript Typescript 错误“类型参数不可分配给类型参数” - Typescript error 'argument of type is not assignable to parameter of type' CustomError类型的参数不能分配给Typescript中的Error - Argument of type CustomError is not assignable to Error in Typescript 如何使用react和typescript修复错误“类型参数(打开:任何)=&gt;布尔值不能分配给布尔类型的参数”? - How to fix error "argument of type (open: any) => boolean is not assignable to parameter of type boolean" using react and typescript? 如何修复 React TypeScript 错误:'""' 类型的参数不可分配给 'SetStateAction 类型的参数<undefined> '</undefined> - How to fix React TypeScript error: Argument of type '""' is not assignable to parameter of type 'SetStateAction<undefined>' 如何修复未定义类型的错误参数不可分配给'字符串或()=&gt;字符串类型的参数使用typescript并做出反应? - How to fix error argument of type undefined is not assignable to parameter of type 'string or () => string usng typescript and react? TypeScript:类型参数不可分配 - TypeScript: Argument of type is not assignable 如何使用 typescript 修复字符串类型的错误参数或未定义的参数不可分配给字符串类型的参数并做出反应? - How to fix the error argument of type string or undefined is not assignable to parameter of type string using typescript and react? 如何使用 react 和 typescript 修复错误“false 类型的参数不可分配给 (currentValue: boolean) =&gt; boolean”? - How to fix error “argument of type false is not assignable to (currentValue: boolean) => boolean” using react and typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM