Why does TypeScript throw an error for this code snippet?
const tryAddress = async (add?: string, postcode?: string) => {
if (postcode?.length < 5) {
// ^^^^^^^^^^^^^^^^ Object is possibly 'undefined'.
However, it doesn't throw an error for this:
const tryAddress = async (add?: string, postcode?: string) => {
if (postcode && postcode.length < 5) {
I was under the impression that the ?.
shorthand should prevent conditions like the one above. Am I overlooking something?
Edit:
Aha! It's because it simplifies to
(undefined < 5)
Is there a more efficient way to write these conditionals since I need them frequently?