Does anyone have a solution for resolving the Typescript error message "Object is possibly 'undefined'" in a section of code that cannot be reached by an undefined value? This area of code is protected by a type guard implemented in a separate function.
type MaybeString = string | undefined;
let a: MaybeString = 'hello';
let b: MaybeString;
function getLength(string: MaybeString) {
if (!isString(string)) {
return -1;
}
return string.length;
}
function isString(string: MaybeString) {
return typeof string === 'string';
}
https://i.sstatic.net/FV1Q5.png https://i.sstatic.net/ppJSQ.png