[英]Type 'HTMLButtonElement' is not assignable to type 'string'.ts(2322)
You have a function that generates "tr" and "td" in the table.您有一个 function 在表中生成“tr”和“td”。 With For Loop I put "td" elements into "tr".
使用 For 循环,我将“td”元素放入“tr”。 Actually Everything works.
实际上一切正常。 But I am getting an error in Visual Studio Code.
但我在 Visual Studio Code 中遇到错误。
Her is my code:她是我的代码:
function setUI(tbody, courseData){
let arrCourse = Object.values(courseData) // parse object to array
let tr = document.createElement('tr'); // create tr
let button = document.createElement('button'); // create delete button
button.className = 'btn btn-danger'; // add class to button element
tbody.appendChild(tr); // add tr to tbody
for(let i = 0; i<arrCourse.length+1; i++){
let td = document.createElement('td'); // create td .
tr.appendChild(td); // append td to tr
td.innerHTML = arrCourse[i]; // get index of array then push it to td
if(i==arrCourse.length+1){
td.appendChild(button);
}
}
} }
My error is here: Type 'unknown' is not assignable to type 'string'.ts(2322)我的错误在这里:类型“未知”不可分配给类型“字符串”.ts(2322)
Erros is this line:错误是这一行:
td.innerHTML = arrCourse[i]; // get index of array then push it to td
Typescript compiler is not able to infer the type, so it assumes the type to be any . Typescript 编译器无法推断类型,因此假定类型为any 。 You can solve this problem by setting the type explicitly
您可以通过显式设置类型来解决此问题
let arrCourse: string[] = Object.values(courseData) // parse object to array
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.