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

reactjs - WebStorm TypeScript variable possibly undefined - Stack Overflow

programmeradmin2浏览0评论

I'm working with TypeScript and React inside WebStorm. WebStorm is warning me "TS18048: b.firstName is possibly undefined" as well as for a.firstName.

I have the below function which works, but as I'm new to TypeScript, I'm having a hard understanding how to get this possible error to go away in WebStorm without using ts-ignore. I'm wondering if there is a better way to do this. I am having similar other things come up like this as well.

Can someone confirm if this is a proper way to build this function or enlighten me on a better approach?

export const sortByFirstName = (arr: Employee[]) => {
    arr.sort((a, b) => {
        if (a.firstName > b.firstName) return 1;
        if (a.firstName < b.firstName) return -1;
        return 0;
    });
};
发布评论

评论列表(0)

  1. 暂无评论