I have this js code in react inside Promise. if ( currentAppeal.getState() == null…
- User
I have this js code in react inside Promise. if ( currentAppeal.getState() == null || appealId !==currentAppeal.getState().id ) { reject({ status: 400, statusText: ‘Id обращения неравен Id активного обращения’, }); return; } Currently I have a TypeScript error stating that ‘Object is possibly ‘null’.’ regarding $currentAppeal.getState(). How to fix this problem
- Assistant
You can fix the TypeScript error by adding a type assertion to tell TypeScript that
currentAppeal.getState()will not return null.Here's an example of how you can do this:
if ( currentAppeal.getState() == null || appealId !== currentAppeal.getState()!.id ) { reject({ status: 400, statusText: ‘Id обращения неравен Id активного обращения’, }); return; }By adding the
!symbol aftercurrentAppeal.getState(), you are telling TypeScript that you are certain thatcurrentAppeal.getState()will not return null. This should resolve the TypeScript error. Just make sure thatcurrentAppeal.getState()will indeed never return null in this context. - User
I have this code. async init() { // Работает return new Promise((resolve, reject) => { iFrameProxy .getData<{ status: IGetUserNetworkStatusResponce; appeals: IAppeal[]; }>('loginInOmnichat', {}) .then(({ data }) => { syncAgentStatusEv(data.status.networkStatus); setAppealList(data?.appeals ?? []); const { networkStatus, offlineStatus } = data.status; const status = { networkStatus }; if (offlineStatus != null) { status.substatusId = offlineStatus.uid; } const appeals = data.appeals.map((appeal) => { const converted = convertAppealDataForCRM(appeal); const dateStart = getFormatedDate(appeal.dateStart * 1000, { defaultFormat: ISO_DATE_FORMAT, }); if (dateStart != null) { converted.dateStart = dateStart; } return converted; }); resolve({ appeals, status }); }) .catch((error) => { reject({ status: error.status }); }); }); }, I need to wrap dateStart != null check in try catch and if it is null then I should write error into console 'dateStart is null'
- Assistant
You can wrap the
dateStart != nullcheck in atry-catchblock to catch any potential errors and write a message to the console ifdateStartis null. Here's how you can modify your code to achieve this:async init() { return new Promise((resolve, reject) => { iFrameProxy .getData<{ status: IGetUserNetworkStatusResponce; appeals: IAppeal[]; }>('loginInOmnichat', {}) .then(({ data }) => { syncAgentStatusEv(data.status.networkStatus); setAppealList(data?.appeals ?? []); const { networkStatus, offlineStatus } = data.status; const status = { networkStatus }; if (offlineStatus != null) { status.substatusId = offlineStatus.uid; } const appeals = data.appeals.map((appeal) => { const converted = convertAppealDataForCRM(appeal); const dateStart = getFormatedDate(appeal.dateStart * 1000, { defaultFormat: ISO_DATE_FORMAT, }); try { if (dateStart != null) { converted.dateStart = dateStart; } else { throw new Error('dateStart is null'); } } catch (error) { console.error(error.message); } return converted; }); resolve({ appeals, status }); }) .catch((error) => { reject({ status: error.status }); }); }); } - User
I have this code in react js. const status: {networkStatus: EUserNetworkStatus} = { networkStatus }; if (offlineStatus != null) { status.substatusId = offlineStatus.uid; } I want for my status const to also have additional property in its type declaration which would be substatusId: number. How do I add it?
- Assistant
To add an additional property
substatusIdto thestatusconstant and update its type declaration to include thesubstatusIdproperty, you can define an interface that specifies the type ofstatus. Here's how you can achieve this:interface IStatus { networkStatus: EUserNetworkStatus; substatusId?: number; // Optional property substatusId of type number } const status: IStatus = { networkStatus }; if (offlineStatus != null) { status.substatusId = offlineStatus.uid; }In the code above, we have defined an interface
IStatusthat includesnetworkStatusof typeEUserNetworkStatusandsubstatusIdas an optional property of typenumber. Then, we have updated the type of thestatusconstant to beIStatusand added thesubstatusIdproperty when setting its value based on the conditionif (offlineStatus != null). This way, thestatusconstant will have an additional propertysubstatusIddefined in its type declaration.