[英]How to assign values to array in object
我正在嘗試將數組復制到對象中的數組中。 我收到此錯誤
Type 'any[]' is not assignable to type '[]'.
Target allows only 0 element(s) but source may have more.
我的對象是
newForm: {formName: string, formId: string, formAttributes:[], formResponses:[]};
我正在復制這個數組-
formAttributeValues=["firstname", "lastname"]
如下:
this.newForm.formAttributes= [...this.formAttributeValues];
這不起作用。 我該如何解決這個問題?
這是因為您在使用打字稿時必須提供正確的類型。 這里 formAttributes 應該是 string[] 類型。 請參考下面的代碼更清晰的理解
// 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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.