Narrow Types with Type Guards

1 min read

Type guards tell TypeScript what the value really is.

function printValue(value: string | number) {
  if (typeof value === "string") {
    console.log(value.toUpperCase());
  } else {
    console.log(value.toFixed(2));
  }
}

More on TypeScript

Recent Tips

Validate on Backend. Always.

Frontend validation improves user experience. It shows instant errors and prevents obvious mistakes like empty fields or invalid email formats. But frontend validation is never security. Anyone can bypass it using DevTools, Postman, or by directly calling your API. That’s why backend validation is mandatory it protects your database and business logic from invalid or malicious data.