简体   繁体   English

Angular 7:类型“void[]”不可分配给类型

[英]Angular 7: Type 'void[]' is not assignable to type

I need to populate document object of the following type.我需要填充以下类型的文档对象。 I am using Angular 7 typescript.我正在使用 Angular 7 打字稿。 I am getting error Type 'void[]' is not assignable to type 'IDocumentDetails[]我收到错误类型 'void[]' 不能分配给类型 'IDocumentDetails[]

export interface IDocumentUpload {
    fileDropEntry: NgxFileDropEntry;
    name: string;
    selectedDocumentItem: { 'Id': number; 'Name': string; }
    selectedDate: Date;

}

    export interface IDocumentDetails {
        name: string;
        file: any;
        documentTypeId: number;
        documentDate: Date;
    }


    export interface IDocuments {
        managerStrategyId: number;
        documentDetails: IDocumentDetails[];
    }

    documents: IDocuments;
    public files: IDocumentUpload[] = [];

I am getting error at this line of code我在这行代码中遇到错误

const documents: IDocumentDetails[] = this.files.map(doc =>

Code代码

const documents: IDocumentDetails[] = this.files.map(doc => {
                return  

                     [{ 
                        file: doc.fileDropEntry.fileEntry,
                        documentTypeId: doc.selectedDocumentItem.Id,
                        name: doc.name,
                        documentDate: doc.selectedDate
                    }];

            });

            this.documents = {managerStrategyId : 0, documentDetails: null};
            this.documents.managerStrategyId =  this.ManagerStrategyId;
            this.documents.documentDetails = documents;

Updated the answer just try if its working?更新答案只是尝试它是否有效?

const documents: IDocumentDetails[] = this.files.map((doc) => {
                return { 
                        file: doc.fileDropEntry.fileEntry,
                        documentTypeId: doc.selectedDocumentItem.Id,
                        name: doc.name,
                        documentDate: doc.selectedDate
                    };

            });

            this.documents = {managerStrategyId : 0, documentDetails: null};
            this.documents.managerStrategyId =  this.ManagerStrategyId;
            this.documents.documentDetails = documents;

Since your return statement is not followed by a bracket '(', you are returning nothing. try to place the curly bracket in the same line with the 'return' word.由于您的 return 语句后面没有括号“(”,因此您什么也没有返回。尝试将大括号与“return”字放在同一行。

Alos, like @JSON Derolu said, your .map function needs to return an object, not an array, so remove the square bracket. Alos,就像@JSON Derolu 所说,你的 .map 函数需要返回一个对象,而不是一个数组,所以删除方括号。

try this code:试试这个代码:

const documents: IDocumentDetails[] = this.files.map(doc => {
  return { // notice just a curly bracket, and in the same line with 'return'
    file: doc.fileDropEntry.fileEntry,
    documentTypeId: doc.selectedDocumentItem.Id,
    name: doc.name,
    documentDate: doc.selectedDate
  };
});

Your const documents is of type IDocumentDetails[] so you will need to return an object of type IDocumentDetails in your map function.您的const documents属于IDocumentDetails[]类型,因此您需要在 map 函数中返回IDocumentDetails类型的对象。

Try this.尝试这个。

const documents: IDocumentDetails[] = this.files.map((doc: IDocumentUpload): IDocumentDetails => {
  return {
    file: doc.fileDropEntry.fileEntry,
    documentTypeId: doc.selectedDocumentItem.Id,
    name: doc.name,
    documentDate: doc.selectedDate
  };
});

I have made your code more strongly typed by specifying the data types.我通过指定数据类型使您的代码具有更强的类型。 Here the doc parameter in the map function is of type IDocumentUpload and the return type of the function is IDocumentDetails which the object you are returning in your map function.这里 map 函数中的doc参数是IDocumentUpload类型,函数的返回类型是IDocumentDetails ,这是您在 map 函数中返回的对象。

Try to cast your response like that:尝试像这样投射你的回应:

const documents: IDocumentDetails[] = this.files.map(doc => {
            return 
                 [{ 
                    file: doc.fileDropEntry.fileEntry,
                    documentTypeId: doc.selectedDocumentItem.Id,
                    name: doc.name,
                    documentDate: doc.selectedDate
                }] as IDocumentDetails[];
        });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM