debugtypescriptCritical
How can I solve the error 'TS2532: Object is possibly 'undefined'?
Viewed 0 times
errorobjecthowts2532thepossiblyundefinedcansolve
Problem
I'm trying to rebuild a web app example that uses Firebase Cloud Functions and Firestore. When deploying a function I get the following error:
This is the function:
I'm just trying to deploy the function to test it. And already searched the web for similar problems, but couldn't find any other posts that match my problem.
src/index.ts:45:18 - error TS2532: Object is possibly 'undefined'.
45 const data = change.after.data();This is the function:
export const archiveChat = functions.firestore
.document("chats/{chatId}")
.onUpdate(change => {
const data = change.after.data();
const maxLen = 100;
const msgLen = data.messages.length;
const charLen = JSON.stringify(data).length;
const batch = db.batch();
if (charLen >= 10000 || msgLen >= maxLen) {
// Always delete at least 1 message
const deleteCount = msgLen - maxLen <= 0 ? 1 : msgLen - maxLen
data.messages.splice(0, deleteCount);
const ref = db.collection("chats").doc(change.after.id);
batch.set(ref, data, { merge: true });
return batch.commit();
} else {
return null;
}
});I'm just trying to deploy the function to test it. And already searched the web for similar problems, but couldn't find any other posts that match my problem.
Solution
With the release of TypeScript 3.7, optional chaining (the
As such, you can simplify your expression to the following:
You may read more about it from that version's release notes, which cover other interesting features released on that version.
Run the following to install the latest stable release of TypeScript.
That being said, Optional Chaining can be used alongside Nullish Coalescing to provide a fallback value when dealing with
Additional points:
If you are using optional chaining in the conditional
The following will fail in strict TypeScript, as you are possibly comparing an undefined value with a number.
Instead, this is what you should be doing:
? operator) is now officially available.As such, you can simplify your expression to the following:
const data = change?.after?.data();You may read more about it from that version's release notes, which cover other interesting features released on that version.
Run the following to install the latest stable release of TypeScript.
npm install typescriptThat being said, Optional Chaining can be used alongside Nullish Coalescing to provide a fallback value when dealing with
null or undefined valuesconst data = change?.after?.data() ?? someOtherData();Additional points:
If you are using optional chaining in the conditional
if statements, you will still need to ensure that you are doing proper value/type equality checking.The following will fail in strict TypeScript, as you are possibly comparing an undefined value with a number.
if (_?.childs?.length > 0)Instead, this is what you should be doing:
if (_?.childs && _.childs.length > 0)Code Snippets
const data = change?.after?.data();npm install typescriptconst data = change?.after?.data() ?? someOtherData();if (_?.childs?.length > 0)if (_?.childs && _.childs.length > 0)Context
Stack Overflow Q#54884488, score: 382
Revisions (0)
No revisions yet.