4

I am trying to copy an array into an array in object. I am getting this error

Type 'any[]' is not assignable to type '[]'.
  Target allows only 0 element(s) but source may have more.  

My object is

newForm: {formName: string, formId: string, formAttributes:[], formResponses:[]};

I am copying this array-

formAttributeValues=["firstname", "lastname"]

as follows:

this.newForm.formAttributes= [...this.formAttributeValues];

This doesn't work. How can I solve this?

0

1 Answer 1

2

This is because you've to give proper types while using typescript. Here formAttributes should be of string[] type. Please refer to the code below for a more clear understanding

// newForm object type definition
let newForm: {formName: string, formId: string, formAttributes:string[], 
formResponses:[]};
// Assigning values to newForm
newForm = {formName: "", formId: "", formAttributes:[], formResponses:[]};
let formAttributeValues=["firstname", "lastname"]
newForm.formAttributes= [...formAttributeValues]
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.