简体   繁体   English

类型 'HTMLButtonElement' 不可分配给类型 'string'.ts(2322)

[英]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.

相关问题 类型“元素”不可分配给类型“字符串”.ts(2322) - Type 'Element' is not assignable to type 'string'.ts(2322) 类型 &#39;string&#39; 不可分配给类型 &#39;(url: string) =&gt; string&#39;.ts(2322) - Type 'string' is not assignable to type '(url: string) => string'.ts(2322) 类型 &#39;string&#39; 不能分配给类型 &#39;{ 模型:{ 节点:[]; 链接:[]; }; }&#39;.ts(2322) - Type 'string' is not assignable to type '{ model: { nodes: []; links: []; }; }'.ts(2322) 键入'字符串 | 号码 | boolean' 不能分配给类型 'undefined'。 类型“字符串”不可分配给类型“未定义”.ts(2322) - Type 'string | number | boolean' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'.ts(2322) 类型“未知”不可分配给类型“从不”.ts(2322) - Type 'unknown' is not assignable to type 'never'.ts(2322) 类型 &#39;undefined&#39; 不能分配给类型 &#39;number&#39; .ts(2322) - Type 'undefined' is not assignable to type 'number' .ts(2322) 类型“编号”不可分配给类型“PersonConfig”。 ts(2322) - Type 'number' is not assignable to type 'PersonConfig'. ts(2322) angular2 TS2322类型&#39;Promise <void> &#39;不可分配给&#39;Promise <string> &#39;当给定字符串时 - angular2 TS2322 Type 'Promise<void>' is not assignable to type 'Promise<string>' when given a string 输入“鼠标事件”<HTMLButtonElement, MouseEvent> &#39; 不可分配给类型 &#39;number&#39;.ts (2345) - Type 'MouseEvent<HTMLButtonElement, MouseEvent>' is not assignable to type 'number'.ts (2345) TypeScript - TS2322:类型“string”不可分配给类型“str1”| “str2”&#39; - TypeScript - TS2322: Type 'string' is not assignable to type '“str1” | “str2”'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM