Typescript “error TS2532: Object is possibly 'undefined'” even after undefined check
I'm trying to use the --strict option on tsc but I ran into the following "weird" case that I don't seem to understand. If I write: function testStrict(input: {query?: {[prop: string]: string}}) { if (input.query) { Object.keys(input.query).forEach(key => { input.query[key]; }) } return input; } the compiler complains about: test.ts(5,9): error TS2532: Object is possibly 'undefined'. (the offending line is input.query[key]; ) What I don't understand is, I have already checked for undefined with the if guard on the first line of the function if (input.query) , so why does the compiler think it