最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript: Argument of type 'string | undefined' is not assignable to parameter of type 's

programmeradmin6浏览0评论

I have list of nestet array list, I am trying fiter based on the array value. When I filter getting error. I am using typescript with eslint.

Argument of type 'string | undefined' is not assignable to parameter of type 'string'

But I checked the null/undefined value but still I am getting error.

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"});

When I use the non-null assertion operator it the error resolved but getting lint issue.

const filterData = arrayData.filter(list => list.includes(user.username!));

Can anyone please help me to fix this issue.

I have list of nestet array list, I am trying fiter based on the array value. When I filter getting error. I am using typescript with eslint.

Argument of type 'string | undefined' is not assignable to parameter of type 'string'

But I checked the null/undefined value but still I am getting error.

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"});

When I use the non-null assertion operator it the error resolved but getting lint issue.

const filterData = arrayData.filter(list => list.includes(user.username!));

Can anyone please help me to fix this issue.

Share Improve this question asked Jan 24, 2021 at 8:16 RSKMRRSKMR 1,8925 gold badges32 silver badges74 bronze badges 1
  • User Interface already defined. so unable to "username?: string | undefined;" – RSKMR Commented Jan 24, 2021 at 8:30
Add a ment  | 

3 Answers 3

Reset to default 7

you are paring the User .username, which is Optional, that means it's of type string | undefined to every string item in an array,

you can change the type of your array to: arrayData: Array<Array<string|undefined>> or you can just use as to treat it as a string, not string|undefined: i.e.: list.includes(user.username as string)

You can declare username like this:

username?: string | undefined;

when you want to set a variable that already has a type | undefined, and the setter doesn't accept any type | undefined, you could use the ! at the end of the variable for example:

for an interface like yours

interface User { id: int; username?: string }

having a variable like

let userVariable = 'Terminator3000'

you can do

user: User = { id: 123, username: userVariable! }

! operator indicates that the value isn't undefined

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论