TypeScript types are only enforced at compile time and do not exist at runtime. Given this, if I define a TypeScript function that accepts a string parameter, is it necessary to perform null checks? This question arises because if the API returns null, it could lead to runtime errors.
Consider the following code snippet:
const capitalise = (val: string) => {
// Question: Is this check redundant due to TypeScript's type checking?
if (!val) {
return '';
}
return val.toUpperCase();
};
// Assume 'name' is fetched from an API or another source that could return null
const name: string = /* fetch from API or another source */;
capitalise(name);
What are the best practices in such scenarios to ensure runtime safety in TypeScript applications?