RSK*_*KMR 3 javascript typescript eslint
我有嵌套数组列表的列表,我正在尝试根据数组值进行调整。当我过滤时出现错误。我正在使用带有 eslint 的打字稿。
Argument of type 'string | undefined' is not assignable to parameter of type 'string'
Run Code Online (Sandbox Code Playgroud)
但我检查了空/未定义值,但仍然收到错误。
interface User {
id: string;
username?: string;
}
function getList (user:User){
if(user.username === undefined){
return {};
}
let arrayData: string[][] = [];
arrayData = [["abcd"],["efgh"],["xyz"],["abcd","xyz"]];
//here i have differnt logic
const filterData = arrayData.filter(list => list.includes(user.username));
return filterData;
}
getList({id: "123", username: "xyz"});
Run Code Online (Sandbox Code Playgroud)
当我使用non-null assertion operator它时,错误已解决,但出现了 lint 问题。
const filterData = arrayData.filter(list => list.includes(user.username!));
Run Code Online (Sandbox Code Playgroud)
谁能帮我解决这个问题。
您正在比较 User ,它是可选的,这意味着它是数组中每个项目.username的类型,string | undefinedstring
您可以将数组的类型更改为:arrayData: Array<Array<string|undefined>>或者您可以将as其视为string,而不是string|undefined: ,即:list.includes(user.username as string)
| 归档时间: |
|
| 查看次数: |
22540 次 |
| 最近记录: |