How to assign values to array in object

Viewed 1469

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?

1 Answers

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]
Related