Repository navigation
strictNullChecks mode - Object is possibly 'undefined'. #10642
Description
Activity
- addedWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
on Aug 31, 2016 RyanCavanaugh commented
on Aug 31, 2016 MemberMore actionsThis is the intended behavior because we don't know that
route.childrenis still not-undefined at the point when the callback executes. It is the case thatforEachexecutes its callback immediately, but this is not true of all functions and we don't know that someone isn't going to removeroute.childrenafter you leave the scope ofregister.A good solution is to capture the property in a const, which we can statically know will still be not-undefined
function register(route: IRoute) { const children = route.children; if (children) { // <-- children is of type IRoute|undefined Object.keys(children).forEach(routeKey => { // <-- children is of type IRoute register(children[routeKey]); // <-- OK }); } }
Reacted by Ruben Péricas-Moya, Sayan Pal, Nicholas J. Markkula, Michael Zalla, Benoit Jehanno, Daniel Neel, kenjiru, Guy Valariola, Anatoliy Siryi, Mher Aghabalyan and 17 moreThanks Ryan Cavanaugh (@RyanCavanaugh) for the fast feedback :) That's true! I never thought of that possibility.
I just enabled strictNullChecks for our project and found these 3 issues (2 I posted earlier) that I couldn't wrap my head around. Truly amazing work 👍
Ryan Cavanaugh (@RyanCavanaugh) for some reason this is not working for 2.0.10 and 2.1+ for me right now.
There got to be something on my side as it was working....
let deferred = defer() setTimeout(() => { if (deferred) { deferred.resolve(1) // <-- still error with Object is possibly 'undefined' } }, 1)
Need some help. 🙇
RyanCavanaugh commented
on Nov 21, 2016 MemberMore actionsHoma Wong (@unional) please post question on Stack Overflow and/or provide a self-contained example
Yes. I can reproduce it:
// index.ts export function defer() { let resolve let reject let promise = new Promise((r, j) => { resolve = r; reject = j; }); return { resolve: resolve, reject: reject, promise: promise }; } let deferred = defer() setTimeout(() => { if (deferred !== undefined) { deferred.reject(1) // <-- in VSC there is no error, but compiles fail } }, 1)
// tsconfig.json { "compilerOptions": { "lib": [ "es2015", "dom" ], "strictNullChecks": true, "target": "es5" }, "include": [ "index.ts" ] }
error:
index.ts(18,5): error TS2532: Object is possibly 'undefined'.Tested with [email protected]
And it turns out the issue is fixed in [email protected] and does not exist in 2.0.10
Reacted by Antoine Rousseau and Subin SibyHave the same issue with typescript (tsc) version 2.3.4
with the process module.process.on('message', (msg) => { console.log('Message from parent:', msg); });but doing:
process.send()does not compile saying:
error TS2532: Object is possibly 'undefined'.Reacted by Maciej Jakub Leks, Atiq and John- added a commit that references this issue
on Jun 18, 2020 Happens to me too :
Vue.directive('click-outside', { bind: (el, binding, vnode) => { const event = ..a function if (vnode) { vnode.context[binding.expression](event) // Object is possibly 'undefined'.ts(2532) } } }Both codium & compile shows the error. Issue being tracked at #10642
- locked as resolved and limited conversation to collaborators
on Jul 8, 2020
In strictNullChecks mode only:
TypeScript Version: nightly (2.0.2)
Code
Expected behavior:
There shouldn't be an error here, because
route.childrenis being checked 2 lines above.Actual behavior:
TypeScript fails to recognize
route.childrencannot be undefined anymore, because of the nested function. It is the same behavior with both function declaration and arrow functions.