0

I've an array of Files (from an input file type) and want to concat them in another array with a different type, but with a File property.

The File array

const files:File[];

The final array type:

export type AttachmentModel = {
    ID?: number;
    Title?: string;
    ServerRelativeUrl?: string;
    File?: File;
}

const attachments:AttachmentModel[];

I've tried to use the spread syntax to merge them, but of course the types are different, so this doesn't work:

const merged = [...attachments, ...files]

If it was only a single object I would have done like this:

var testFile: File;
var testeAttachment: AttachmentModel = { ...{ ...testFile, File: testFile } };

How can I use "destructuring" in the files array so it returns an array of type AttachmentModel?

2
  • 2
    use Array.map Commented Jun 17, 2022 at 12:57
  • Well even without knowing how to do that, what would you expect from your code ? Shouldn't you link the attachements and the files together ? What's the linking key ? Commented Jun 17, 2022 at 12:59

1 Answer 1

3

It looks like you want to end up with a array of AttachmentModel. If so, you'll want to map your File object to AttachmentModel objects:

const merged: AttachmentModel[] = [
    ...attachments,
    ...files.map((file) => ({File: file}))
];

Playground link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.