find方法上没有对象时出现打字稿错误

问题描述

当我的应用程序中没有房地产产品时,就没有房地产对象,然后当我trt时

找不到products.find((x:any) => x.code === 'realestate')时,我在这里遇到打字错误,该如何解决

let REItemsGeos: string[] = response.products.find((x:any) => x.code === 'realestate').items.map((({ geo }) => geo))

这是跳过错误的变量...

解决方法

尝试在映射之前检查您的产品是否存在:

let product = response.products.find((x:any) => x.code === 'realestate');
if (product )
   let REItemsGeos: string[] = product.items.map((({ geo }) => geo)) 
,

使用可选的链接(union WM_CHAR_message { struct { UINT repatCount : 16; UINT scanCode : 8; UINT : 4; UINT contextCode : 1; UINT previousState : 1; UINT transitionState : 1; }; UINT raw; }; LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { union WM_CHAR_message msgu; //C++ safe memcpy(&msgu,&message,sizeof(msgu)); // will be optimized to the store instruction only switch (message) { // ... case WM_CHAR: switch(msgu.scanCode) { //.... } OnKeyPress(wParam); break; default: return DefWindowProc(hwnd,message,wParam,lParam); } return 0; } )运算符。仅当find的值不为null / undefined时,才会进入items.map。

?.