Encountering a Typescript error stating that
'<array name>' is possibly undefined
while attempting to map over an array in Typescript. Below is an example code snippet triggering this issue:
type obj = {
list?: string[]
};
function demonstration(ex: obj) {
ex.list?.map((str) => {
ex.list.findIndex(s => s === str); // error here
})
}
Aware of the workaround involving adding an if check at the beginning of the map function, but keen on understanding the root cause behind this error.
If the list's map function does run, doesn't it imply that the list exists already? Speculation around it being a potential Typescript bug arises due to lack of clarity as a newcomer to Typescript. Appreciate any guidance provided.