Always perform explicit null and undefined checks before accessing object properties or using values in operations. Filter out undefined values from arrays and collections to prevent runtime errors and unexpected behavior.
Always perform explicit null and undefined checks before accessing object properties or using values in operations. Filter out undefined values from arrays and collections to prevent runtime errors and unexpected behavior.
Key practices:
typeof value === "object" && value !== null
)"tool_call_id" in msg
)Example:
// Good: Explicit null check prevents runtime errors
if (typeof value === "object" && value !== null) {
// Safe to access properties
}
// Good: Check property existence and filter undefined values
const toolMessages = tailMessages
.filter(msg => "tool_call_id" in msg)
.map(msg => msg.tool_call_id)
.filter(id => id !== undefined);
// Good: Validate before property access
if (maybeLastAssistantMessageUsage) {
const allMatch = Object.entries(currentUsage).every(
([key, value]) => maybeLastAssistantMessageUsage[key] === value
);
}
Enter the URL of a public GitHub repository