Struggling with TypeScript version 2.8.3, I'm confused as to why the code below is failing to recognize that params is defined inside the if block.
const testFunction = (params?: string) => {
const paramIsDefined = typeof params !== 'undefined';
if (paramIsDefined) {
console.log(params.length);
}
};
An error message pops up: TS2532: Object is possibly 'undefined' on the console.log line for the params variable.
However, this alternate code snippet works as expected:
const testFunction = (params?: string) => {
if (typeof params !== 'undefined') {
console.log(params.length);
}
};
I can't seem to grasp what I am missing or doing incorrectly. Any insights?