Always validate that values exist and have expected properties before accessing them, especially when removing non-null assertions or performing type casting. Avoid making assumptions about value presence or type safety without explicit checks.
Key practices:
Example from the discussions:
// Risky - assumes route exists after removing non-null assertion
const route = this.looseRoutesById[d.routeId]
// Better - validate before use
const route = this.looseRoutesById[d.routeId]
if (!route) return
// Risky - Boolean filter excludes valid falsy numbers like 0, -1
paths.filter(Boolean).join('/')
// Better - explicit check for meaningful values
paths.filter((val) => val !== undefined && val !== null).join('/')
This prevents runtime errors from null reference exceptions and ensures code behaves correctly with edge case values.
Enter the URL of a public GitHub repository